Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the SendMessage Equivalent in wxWidgets

Tags:

wxwidgets

I want to send synchronous event from a worker thread to the UI Main thread. How do I do it in wxWidgets? A link to a sample would be really helpful

like image 568
Canopus Avatar asked Feb 27 '23 07:02

Canopus


2 Answers

You should use QueueEvent(wxEvent* event) for interthread communication.

void wxEvtHandler::QueueEvent(wxEvent* event)

wxDocumentation states:

QueueEvent() can be used for inter-thread communication from the worker threads to the main thread, it is safe in the sense that it uses locking internally and avoids the problem mentioned in AddPendingEvent() documentation by ensuring that the event object is not used by the calling thread any more. Care should still be taken to avoid that some fields of this object are used by it, notably any wxString members of the event object must not be shallow copies of another wxString object as this would result in them still using the same string buffer behind the scenes.

You can do it this way:

wxCommandEvent* evt = new wxCommandEvent();

// NOT evt->SetString(str) as this would be a shallow copy
evt->SetString(str.c_str()); // make a deep copy

wxTheApp->QueueEvent( evt ); 

Hope this will help.

like image 91
ezpresso Avatar answered Mar 05 '23 07:03

ezpresso


AddPendingEvent - This function posts an event to be processed later. http://docs.wxwidgets.org/2.8/wx_wxevthandler.html#wxevthandleraddpendingevent

ProcessEvent - Processes an event, searching event tables and calling zero or more suitable event handler function(s). http://docs.wxwidgets.org/2.8/wx_wxevthandler.html#wxevthandlerprocessevent

wxFrame * frame = new wxFrame(...);
...
wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, ID_MY_BUTTON);
frame->AddPendingEvent(event);

Regarding how to use this from worker thread - You'd rather take a look at Job Queue http://wxforum.shadonet.com/download.php?id=673

like image 27
T-Rex Avatar answered Mar 05 '23 08:03

T-Rex