WxPanel from 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
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

WxPanel from thread

Post by Nico » Wed Feb 21, 2018 7:17 pm

Hello,

I have a wxPanel that takes a while to load, because the content of the panel depends on a configuration file that is parsed in the constructor. To avoid annoying users, I want to create this wxPanel in a worker thread. Once the panel is created I would like to it to be added into a wxNotebook in the main thread.

I tried the straight forward approach of calling notebook->AddPage() from the thread, but that fails. I then found a post in which it was explained that wxWidgets objects can not be added from another thread than the main thread. There it was suggested to create a (costume) event to transfer the data to the main thread and then display it there.
So now I am looking for a way to create the wxPanel in a helper thread and send it to the main thread as payload of an event. The event handler should then add it to the wxNotebook.

My question: can I send a complete wxPanel as payload of an event and if so, how should this be done?

I already managed to create an event in a separate thread and have the corresponding event handler update the screen accordingly, but how can I add the payload and what are the limitations?

Thanks in advance, Nico

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

Re: WxPanel from thread

Post by doublemax » Wed Feb 21, 2018 9:23 pm

My question: can I send a complete wxPanel as payload of an event and if so, how should this be done?
Don't create a wxPanel (or any GUI element for that matter) in a worker thread.
I have a wxPanel that takes a while to load, because the content of the panel depends on a configuration file that is parsed in the constructor.
What's taking so long here and how long does it take? The parsing of the configuration file? The creation of the individual children?

If the parsing takes so long, do the parsing in a worker thread, collect all the information in a suitable data structure and send that to the main thread through an event.

If the creation of the children takes so long, how many are there? Even creating a very complex GUI shouldn't take longer than half a second, i would say that's an acceptable waiting time. If it takes up to 2 seconds, just display a busy cursor. If it takes longer, you may have to think about whether your user interface is reasonable.
Use the source, Luke!

Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Re: WxPanel from thread

Post by Nico » Thu Feb 22, 2018 8:22 pm

Thank you, that is a completely different approach for solving the problem. In fact I don't think the GUI is all that complex, so you are right the majority of the time will be spend loading and processing the configuration files. In my mind this was directly connected to the GUI being loaded and in my current implementation it is, but it doesn't have to be.

Kind regards, Nico

Post Reply