GUI thread --> Main Thread?

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
fbronx
Knows some wx things
Knows some wx things
Posts: 38
Joined: Tue Nov 29, 2005 8:37 am
Location: Belgium

GUI thread --> Main Thread?

Post by fbronx » Tue Jun 22, 2010 10:35 am

Is it necessary that the GUI thread is the same as the main thread? Is it allowed to create a new thread, and do all the GUI stuff in this new thread?
Use wxWidgets in JavaScript: GLUEscript

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2293
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: GUI thread --> Main Thread?

Post by evstevemd » Tue Jun 22, 2010 11:24 am

fbronx wrote:Is it necessary that the GUI thread is the same as the main thread? Is it allowed to create a new thread, and do all the GUI stuff in this new thread?
I have note done threading yet. but once I wanted to do and AFAIK, GUI stuffs MUST be in Main thread and other works can be done in worker thread :D
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

tigerbeard
Experienced Solver
Experienced Solver
Posts: 64
Joined: Sat Oct 07, 2006 1:56 pm

Re: GUI thread --> Main Thread?

Post by tigerbeard » Tue Jun 22, 2010 11:55 am

fbronx wrote:Is it necessary that the GUI thread is the same as the main thread? Is it allowed to create a new thread, and do all the GUI stuff in this new thread?
I've done a lot with theads, but never tried that. Maybe because it would mean that consequently all GUI stuff needed to be in that thread including the whole event chain. wxApp would remain in the main thread.
Its not possible to have GUI related actions in two different threads, because the GUI parts are not thread safe (I've had loads of trouble with wxString. And thats easy compared to GUI). I do remember some forum discussions about doing limited GUI stuff from a thread, and there were workarounds that allowed you to do that. Hover, mostly that leads you to designs that are very difficult to handle.

So before diving into the nuts and bolts to achieve that one should be sure that this is the most sensible option. What is still unclear to me: if you plan to have 2 threads what is the advantage of one over the other or in other words which advantage to you gain from having the GUI in a parallel thread instead of the main thread? Previous discussions always wanted to have multiple threads to do GUI work, e.g. a worker thread controlling its own dialog etc. But thats not what you were asking about.
System config: Xubuntu16.04/Win7/XP/2K, wxWidgets 2.9.3, C::B, GCC, VC

fbronx
Knows some wx things
Knows some wx things
Posts: 38
Joined: Tue Nov 29, 2005 8:37 am
Location: Belgium

Re: GUI thread --> Main Thread?

Post by fbronx » Tue Jun 22, 2010 1:03 pm

tigerbeard wrote:
fbronx wrote:So before diving into the nuts and bolts to achieve that one should be sure that this is the most sensible option. What is still unclear to me: if you plan to have 2 threads what is the advantage of one over the other or in other words which advantage to you gain from having the GUI in a parallel thread instead of the main thread? Previous discussions always wanted to have multiple threads to do GUI work, e.g. a worker thread controlling its own dialog etc. But thats not what you were asking about.
My problem is that a TCPServer object is created in a script. TCPServer creates a new thread to handle incoming requests. Because my main method immediately exits, the TCPServer is also destroyed. The solution I was thinking about: compile/run the script in a separate thread, and let that thread wait for the ending of the TCPServer thread. But when my script is run in a separate thread and I use wxWidgets, all GUI stuff will be handled in this script thread. Will this work?
Use wxWidgets in JavaScript: GLUEscript

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2293
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: GUI thread --> Main Thread?

Post by evstevemd » Tue Jun 22, 2010 1:21 pm

fbronx wrote:
tigerbeard wrote:
fbronx wrote:So before diving into the nuts and bolts to achieve that one should be sure that this is the most sensible option. What is still unclear to me: if you plan to have 2 threads what is the advantage of one over the other or in other words which advantage to you gain from having the GUI in a parallel thread instead of the main thread? Previous discussions always wanted to have multiple threads to do GUI work, e.g. a worker thread controlling its own dialog etc. But thats not what you were asking about.
My problem is that a TCPServer object is created in a script. TCPServer creates a new thread to handle incoming requests. Because my main method immediately exits, the TCPServer is also destroyed. The solution I was thinking about: compile/run the script in a separate thread, and let that thread wait for the ending of the TCPServer thread. But when my script is run in a separate thread and I use wxWidgets, all GUI stuff will be handled in this script thread. Will this work?
You can do all non gui stuffs on threads and then report to GUI thread which will then process. Thereby making each request as detached thread which will easy the load of managing thread clean ups
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

tigerbeard
Experienced Solver
Experienced Solver
Posts: 64
Joined: Sat Oct 07, 2006 1:56 pm

Re: GUI thread --> Main Thread?

Post by tigerbeard » Tue Jun 22, 2010 3:46 pm

fbronx wrote: My problem is that a TCPServer object is created in a script. TCPServer creates a new thread to handle incoming requests. Because my main method immediately exits, the TCPServer is also destroyed. The solution I was thinking about: compile/run the script in a separate thread, and let that thread wait for the ending of the TCPServer thread. But when my script is run in a separate thread and I use wxWidgets, all GUI stuff will be handled in this script thread. Will this work?
When you can wait in a thread for another one to end, why is it not possible to wait in you main thread for the same thing? Maybe I do not know enough about running scripts in a wxApp. As you say the App ends immediately. Maybe that can be changed.
Anyway, if thats resolved by running the Server creation part in a separate thread, can't you just run the server creation bit in that thread and continue with the rest in you main thread as normally. You could react on messages posted from both threads before they end and react in you GUI.
System config: Xubuntu16.04/Win7/XP/2K, wxWidgets 2.9.3, C::B, GCC, VC

Post Reply