Custom main loop possible?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
erikh
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Sep 16, 2020 12:17 am

Custom main loop possible?

Post by erikh » Wed Sep 16, 2020 1:46 am

Hello everyone!

After a break of some years i´m currently trying to get back to some coding in my spare time and want to rewrite some of my old stuff (plain WinAPI) to get used to coding again. I also want to improve things while rewriting and so i thought it would be a good idea to make the new version also platform independent. Of course, i stumbled upon wxwidgets as a possible solution for this. The fact that it also provides support for threads and networking is also pretty welcome.

So, i picked an old set of Client/Server/Management applications that i want to rewrite. and started to read tutorials and look up the reference of wxwidgets. but all tutorials that i have found so far are using the standard approach of using IMPLEMENT_APP() and let wxwidgets control the application flow entirely.

Sadly, thats not what i need. The mentioned applications are mostly network and/or calculation-result driven. So, like in the old winapi way, i would need just a custom main loop, in which i can pass the gui messages to a handler, process network traffic and manage a bunch of different other threads.

after some further searches on the internet, i couldn´t find a real solution to my problem. of course i found some other threads, even on this forum, but not a single one doesnt call the wxwidgets own message loop at some point. (also, imo the official class reference is just horrible. i looked for a solution there and no function in the sections i looked up get a helpfull description of what they do and how to use them correctly to achive certain things) so i registered for the forum and here i am with my questions.


i already stumbled across the IMPLEMENT_APP_NO_MAIN macro (that isnt even listed on the official documentation??) which allows me to use my own entry point. but where do i continue from that? whats the correct way to initialize wxwidgets and bring it to work?

is it even possible to manually poll single events from a event queue and pass it to a handler? (like the winapi approach)? an uncontrollable loop, that handles the queue itself is not an option, because of the network driven approach and the fact, that the gui is mostly only a monitoring interface with some very limited interaction options.

i read somewhere that it should be possible to run the gui in a seperate thread. that would be a good alternative, too. but also, no solution on how to do this correctly.

so i would be very thankfull if someone could help me out on my question or at least tell me if it is even possible what i want to do with wxwidgets. otherwise i would need to look for an alternative, before investing time on this.


best regards, erik

User avatar
doublemax
Moderator
Moderator
Posts: 15069
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom main loop possible?

Post by doublemax » Wed Sep 16, 2020 8:22 am

The mentioned applications are mostly network and/or calculation-result driven. So, like in the old winapi way, i would need just a custom main loop, in which i can pass the gui messages to a handler, process network traffic and manage a bunch of different other threads.
I don't quite understand why you can't use the "normal", event driven approach. Rather than pulling an event and dispatching it yourself, you assign a handler for the event you're interested beforehand. I've been using wxWidgets for many years now and never had to use a custom event loop. Maybe you're just taking a wrong approach here?
i read somewhere that it should be possible to run the gui in a seperate thread. that would be a good alternative, too. but also, no solution on how to do this correctly.
The "dll" sample shows how to do it, the code is MSW specific, but could be adjusted for other platforms with enough knowledge about the target platform.

Anyway: Looking through the documentation, creating your own event loop seems straight forward:
- subclass wxEventLoopBase and implement all abstract methods
- Save the pointer to the current event loop using wxEventLoopBase::GetActive
- Set your own event loop using wxEventLoopBase::SetActive
- if you're done restore the old event loop

You can use the Windows implementation of the event loop as inspiration:
https://github.com/wxWidgets/wxWidgets/ ... vtloop.cpp

Of course, i've never done this, so there might be some challenges i'm not aware of.

This might help, too, even if it doesn't use a custom event loop:
viewtopic.php?p=177574#p177574

And then you can always ask on the wx-users group where you can reach the core wx developers, this here is a user forum:
https://groups.google.com/forum/#!forum/wx-users
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4364
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Custom main loop possible?

Post by ONEEYEMAN » Wed Sep 16, 2020 6:58 pm

Hi,
i read somewhere that it should be possible to run the gui in a seperate thread. that would be a good alternative, too. but also, no solution on how to do this correctly.
If you are talking about MT approach - NO, NO and NO.

It is NOT POSSIBLE to have a GUI in the secondary thread.


But if you are talking about DLL - follow doublemax advice.

Thank you.

erikh
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Sep 16, 2020 12:17 am

Re: Custom main loop possible?

Post by erikh » Thu Sep 17, 2020 6:54 am

Hello everyone!

Thanks so far for your answers!
doublemax wrote:
Wed Sep 16, 2020 8:22 am
I don't quite understand why you can't use the "normal", event driven approach. Rather than pulling an event and dispatching it yourself, you assign a handler for the event you're interested beforehand. I've been using wxWidgets for many years now and never had to use a custom event loop. Maybe you're just taking a wrong approach here?
As i tried to explain, the application is just purely network and calculation driven. With that i mean, that i´m waiting for network messages and based on what message comes in, the application executes certain tasks, launch other worker threads and sends results back to its clients. So basically a standard Server application. The gui´s only there so i can manage and monitor the server easily. So for me it wouldnt make much sense to let a main message loop for the gui control the app based on the gui events. Sure i could try to poll network data in this loop also, but that just doesnt seem right. What happens if there is no gui interaction for a longer time, like some days? will the loop block, waiting for a gui event to happen?
doublemax wrote:
Wed Sep 16, 2020 8:22 am
The "dll" sample shows how to do it, the code is MSW specific, but could be adjusted for other platforms with enough knowledge about the target platform.
Do you refer to the example in the "/samples/dll/" folder that comes with wxwidgets? I will definitly take a look there and see how that works. So thanks for that hint!
doublemax wrote:
Wed Sep 16, 2020 8:22 am
Anyway: Looking through the documentation, creating your own event loop seems straight forward:
- subclass wxEventLoopBase and implement all abstract methods
- Save the pointer to the current event loop using wxEventLoopBase::GetActive
- Set your own event loop using wxEventLoopBase::SetActive
- if you're done restore the old event loop

Thats actually not what i want to do. I dont want to overwrite the wx loop by another one. in that case the loop would still be called and managed by wx, based on what gui events are coming in. that would surely create other problems with the apps main tasks in some way.


I will of course check the other Links you posted - Thanks!!
ONEEYEMAN wrote:
Wed Sep 16, 2020 6:58 pm
If you are talking about MT approach - NO, NO and NO.

It is NOT POSSIBLE to have a GUI in the secondary thread.

But if you are talking about DLL - follow doublemax advice.

Thank you.
ok, thats now like the exact opposite of @doublemax´s answer.

in this way, i would completly push all gui related stuff in a single, dedicated thread for that. (of course i wouldnt split the gui over several threads, that would surely become an even bigger problem) - maybe that would be the best approach anyway. so wx could run its own standard event loop and manage itself and i wouldnt need to get around that.

so, is it possible or not? i´m quite confused now.

anyway, i will check the "dll" sample and the other links provided later today and see if those will help.

So, thank you very much for your help!

Best regards, erik

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4364
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Custom main loop possible?

Post by ONEEYEMAN » Thu Sep 17, 2020 7:25 am

Hi,
Since we are now know what type of application you are developing...

Don't bother looking at the dll sample.
Check the socket sample instead.

Build both client and server, run it and check their code.

Also as I said GUI absolutely must to run in main thread. By main thread I mean the thread that creates wxApp-based object.
No secondary thread/worker thread should know about GUI.

You have 2 way to update it - either send an wxThreadEvent or use wxTimer (someone will correct me if I'm wrong).

Sending the event is probably the preferred way.

Thank you.

BTW, if there is nothing coming from the network, you application will just sit there. Nothing will be blocked.
But I would suggest to let everything be handled by wx.

User avatar
doublemax
Moderator
Moderator
Posts: 15069
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom main loop possible?

Post by doublemax » Thu Sep 17, 2020 7:58 am

With that i mean, that i´m waiting for network messages and based on what message comes in, the application executes certain tasks, launch other worker threads and sends results back to its clients. So basically a standard Server application. The gui´s only there so i can manage and monitor the server easily.
Do you intend to rewrite all this using wxWidgets methods, e.g. wxSocket, or do you want to keep all your current code? It sounds like you prefer the latter?

In the former case, you can all do this with the standard wxWidgets event loop. The "sockets" sample that comes with wxWidgets shows a socket based client/server application. wxSockets support event driven and "blocking" mode.

In that latter case it might really be the best to move the GUI parts in a separate thread. In wxWidgets all accesses to GUI elements must happen in the "main" thread, but the "main" thread is the thread in which wxWidgets was initialized. That's what the "DLL" sample does. (Yes, the one in <wxdir>/samples/dll). So in your case, you can't make GUI calls directly from within your code (e.g. changing a label in a wxStaticText), you'd have to send an event to the GUI thread telling it what to do.
Use the source, Luke!

erikh
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Sep 16, 2020 12:17 am

Re: Custom main loop possible?

Post by erikh » Sat Sep 19, 2020 6:33 am

doublemax wrote:
Thu Sep 17, 2020 7:58 am
Do you intend to rewrite all this using wxWidgets methods, e.g. wxSocket, or do you want to keep all your current code? It sounds like you prefer the latter?
I´m not so sure about this. My primary intention on using wxwidgets is its multi OS support. So to become os independent it probably is the best to start from scratch and rewrite everything with wx widgets.

ONEEYEMAN wrote:
Thu Sep 17, 2020 7:25 am
Don't bother looking at the dll sample.
Check the socket sample instead.
Ok, i will definitly do this - thanks for the hint ! But i will check the dll sample also and see how all this works.

So thanks for all your tips and advises.

And another question came to my mind yesterday: since its support multiple OS - do i need to recompile the application for another OS ( most likely linux) or can i just push the application to the other os and it will run? i have never ( exept for a handfull of some real basic stuff yeeeeeaaaars ago) done any real programming there and lacking knowledge in this. and if so, do i need to recompile it on the target platform or can i compile it from windows (visual studio) and then push it to the target system? (so i can keep my build environment & settings and don´t need to recreate this on another platform) Maybe some of you can point me to some tuts/articles about this.

Best Regards, Erik

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4364
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Custom main loop possible?

Post by ONEEYEMAN » Sat Sep 19, 2020 6:42 am

Hi,
Yes, you will have to recompile the library and your application for *nix.
You cant just run Windows executable on *nix unless you are running Wine.

So yes - recompile everything.

Thank you.

Post Reply