Hi.
Please bear with me, I am a wxWidgets noob.
What is the preferred / modern way to get values from the UI from within another c++ std::thread?
I have multiple controls (radiobuttons, text inputs, sliders) that I would like to get the value of and use in some worker threads that do some hw simulation and MQTT io operations.
My mindset is damaged by windows forms and javascript, in there i could expose an event with an object aggregating the "new" ui state with all values. Any change of a changed property results in the generation of a new state object (or a shared object with a mutex) - and subscribing to the event would give me a new/refreshed object on clientside with the updated values/properties. Not very efficient but very clean to document to a larger team. "Subscribe to this event and you get the new state of the ui - please remember to lock on read and be quick about it or clone the obj.".
If callafter have (does it?) an async thread safe variation with a return value of sorts then I could call "UI.GetSimXUIState" or "UI.UpdateSimXUIState(uistate&) and get something nice to work on in the simulator threads.
This might be noob, but finding the up to date ways to do things is a bit difficult. But - how to get them vals out?
wxWidgets is a nice experience, once things start to come clear
What is the preferred way of getting a gui value from other std::thread
-
- In need of some credit
- Posts: 1
- Joined: Mon May 06, 2019 11:01 am
-
- Moderator
- Posts: 1618
- Joined: Wed Nov 12, 2008 7:23 am
- Location: Romania
Re: What is the preferred way of getting a gui value from other std::thread
One way to do it:
Directly using event handlers or CallAfter() somehow from the secondary thread cannot be solutions.
- from the secondary thread send a wxThreadEvent to the main thread requesting the needed info;
- secondary thread waits at a wxMessageQueue;
- main thread posts the info to the wxMessageQueue instance;
- secondary thread is unlocked and processes the info.
Directly using event handlers or CallAfter() somehow from the secondary thread cannot be solutions.
-
- Super wx Problem Solver
- Posts: 329
- Joined: Tue Oct 18, 2016 2:31 pm
Re: What is the preferred way of getting a gui value from other std::thread
straightforward and the best way is
1. thread T must have his personal wxMessageQueue _queue.
2. gui for example wxButton must put wxMessage to _queue, if button changed state. thread must wait on this queue, get incoming event, and ignore or somehow react on it.
3. if you need a lot of such buttons, better to write your class kinda "wxButtonThreadSupplier", implement handler, putting messages in given thread, and use them instead of regular button.
1. thread T must have his personal wxMessageQueue _queue.
2. gui for example wxButton must put wxMessage to _queue, if button changed state. thread must wait on this queue, get incoming event, and ignore or somehow react on it.
3. if you need a lot of such buttons, better to write your class kinda "wxButtonThreadSupplier", implement handler, putting messages in given thread, and use them instead of regular button.
ubuntu 20.04, wxWidgets 3.2.1
-
- Moderator
- Posts: 19164
- Joined: Fri Apr 21, 2006 8:03 pm
- Location: $FCE2
Re: What is the preferred way of getting a gui value from other std::thread
Do you need this information while the thread is running or just at the start? The latter case is what you need 99% of the time. In that case, just collect all the information the thread needs in a custom class, fill it, and pass it to the thread ctor.I have multiple controls (radiobuttons, text inputs, sliders) that I would like to get the value of and use in some worker threads that do some hw simulation and MQTT io operations.
Use the source, Luke!