I have worked with threads in wxWidgets in almost all of the ways described below, and I can say that using custom events, while initially a bit more complicated, will save you some headache in the long run. (The wxMessageQueue class is pretty nice, but when I used it, I found that it was leaking, I did not check it in about a year.)
Basic example:
Myfrm.cpp
#include "MyThread.h" BEGIN_EVENT_TABLE(MyFrm,wxFrame) EVT_COMMAND(wxID_ANY, wxEVT_MYTHREAD, MyFrm::OnMyThread) END_EVENT_TABLE() void MyFrm::PerformCalculation(int someParameter){
Mythread.h
#ifndef MYTHREAD_H #define MYTHREAD_H #include <wx/thread.h> #include <wx/event.h> BEGIN_DECLARE_EVENT_TYPES() DECLARE_EVENT_TYPE(wxEVT_MYTHREAD, -1) END_DECLARE_EVENT_TYPES() class MyThread : public wxThread { public: MyThread(wxEvtHandler* pParent, int param); private: int m_param; void* Entry(); protected: wxEvtHandler* m_pParent; }; #endif
Mythread.cpp
#include "MyThread.h" DEFINE_EVENT_TYPE(wxEVT_MYTHREAD) MyThread::MyThread(wxEvtHandler* pParent, int param) : wxThread(wxTHREAD_DETACHED), m_pParent(pParent) {
I feel this is a much more understandable and concise example than the one offered by the wiki. Obviously, I left the code regarding the actual launch of the application (the wx convention will do this MyApp.cpp) and any other non-thread related code.
source share