What is the preferred way of getting a gui value from other std::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.
Laro88
In need of some credit
In need of some credit
Posts: 1
Joined: Mon May 06, 2019 11:01 am

What is the preferred way of getting a gui value from other std::thread

Post by Laro88 »

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 :D
catalin
Moderator
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

Post by catalin »

One way to do it:
  1. from the secondary thread send a wxThreadEvent to the main thread requesting the needed info;
  2. secondary thread waits at a wxMessageQueue;
  3. main thread posts the info to the wxMessageQueue instance;
  4. secondary thread is unlocked and processes the info.
Formats of request, returned info etc. are up to you.

Directly using event handlers or CallAfter() somehow from the secondary thread cannot be solutions.
alys666
Super wx Problem Solver
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

Post by alys666 »

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.
ubuntu 20.04, wxWidgets 3.2.1
User avatar
doublemax
Moderator
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

Post by doublemax »

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.
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.
Use the source, Luke!