What is the SendMessage Equivalent in wxWidgets
I want to send synchronous event from a worker thr开发者_开发百科ead to the UI Main thread. How do I do it in wxWidgets? A link to a sample would be really helpful
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.
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
ProcessEvent - synchronous event handling. It's declared as SendMessage equivalent. But as I tested it does NOT switch to widget's thread, it processes in current thread!
AddPendingEvent and QueueEvent - variations of PostMessage (asynchronous event handling).
精彩评论