Wxwidgets callback

I would like to add a callback function to a streaming function without freezing the main application.

Example: when I click on a button, it launches a stream function. I want to inform the user about the completion of work.

thanks

cs functions; pthread_t thread; pthread_create(&thread, NULL, maFonction, (void*)&functions); //pthread_join(thread, NULL); 

pthread_join blocks the main application, waiting for the thread to finish. So how would I do it. thanks a lot

+4
source share
2 answers

Make the thread disconnected by calling pthread_detach() on the child thread or by creating the thread in the main thread, set the pthread attributes for this thread to a separate state. Now that the thread is disconnected, you do not need to call pthread_join() on the main thread. Then, in the child thread itself, before exiting the thread, click the event in the event queue of the WxWidgets object that generated the thread to β€œdeclare” that the child thread has completed. Finally, add an event handler for the thread completion event to your WxWidget object to handle even the generated thread by placing an event queue in it.

For example, you can create an event such as THREAD_FINISHED_EVENT , which the thread will click on the event queue of the object that will generate the threads. The code will look like this:

 wxCommandEvent event(THREAD_FINISHED_EVENT, GetId()); //"this" points to the parent WxWidgets object spawning the threads //and allows you to access the "this" pointer in the handler event.SetEventObject(this); //Send the event this->AddPendingEvent(event); 

The event itself will be processed in the main WxWidget event stream, which sets a handler for the event. You just need to provide a handler for the WxWidget object and define the event itself. This can be done using the DEFINE_EVENT_TYPE macro, and then add the following construct to the constructor of the WxWidget object, which will spawn the threads themselves:

 //myWxWidget::thread_handler is the handler for your thread ending events Connect(widgetID, THREAD_FINISHED_EVENT, wxCommandEventHandler(myWxWidget::thread_handler)) 

To summarize this, here is what some theoretical WxWidgets object class looks like:

 //myWindowThreadClass.hpp #include <wx/wx.h> #include <wx/event.h> extern expdecl const wxEventType THREAD_FINISHED_EVENT; class myWindowThreadClass: public wxWindow { public: myWindowThreadClass(wxWindow* parent, int id); //handler for the thread-ending event void thread_handler(wxCommandEvent& event); //pushes a thread event on the wxWidgets event-queue //for myWindowThreadClass void send_thread_event(); }; //myWindowThreadClass.cpp #include <myWindowthreadClass.h> #include <pthread.h> const wxEventType THREAD_FINISHED_EVENT = wxNewEventType(); void* thread_func(void* data) { myWindowThreadClass* window_ptr = static_cast<myWindowThreadClass*>(data); //detach thread pthread_detatch(pthread_self()); //... rest of thread function window_ptr->send_thread_event(); return (void*)0; } myWindowThreadClass::myWindowThreadClass(wxWindow* parent, int id): wxWindow(parent, id) { //enable the event handler Connect(id, THREAD_FINISHED_EVENT, wxCommandEventHandler(myWindowThreadClass::thread_handler)); //create your threads pthread_t tid; for (int i=0; i < NUM_THREADS; i++) { pthread_create(&tid, NULL, thread_func, this); } //...do anything else needed to initialize object } void myWindowThreadClass::thread_handler(wxCommandEvent& event) { //handle the event } void myWindowThreadClass::send_thread_event() { wxCommandEvent event(THREAD_FINISHED_EVENT, GetId()); event.SetEventObject(this); //Send the event ... import to use this function, as it will cause //the event to be processed in main-thread, not spawned child threads this->AddPendingEvent(event); } 
+4
source

Set a variable, for example. xxx_is_done , to false before the start of the stream. And when the thread executes, the last thing it does is set xxx_is_done to true . Then just check the variable in the loop of the main event and call pthread_join in the thread when the variable is true, and also return the variable to false so that you don't call pthread_join in the thread again.

+1
source

All Articles