From 055fa773bfaee218f97f62abc80a9711e6e128fa Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 28 Jun 2015 16:42:02 +0200 Subject: [PATCH] Simplify wxThreadHelper example to use Bind(). Avoid confusing people with (unnecessary in this case) custom event types and make the example code simpler and safer. --- interface/wx/thread.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/interface/wx/thread.h b/interface/wx/thread.h index 17c68a84f4..5e6b0f29fb 100644 --- a/interface/wx/thread.h +++ b/interface/wx/thread.h @@ -303,12 +303,15 @@ public: Example: @code - wxDECLARE_EVENT(myEVT_THREAD_UPDATE, wxThreadEvent); - class MyFrame : public wxFrame, public wxThreadHelper { public: - MyFrame(...) { ... } + MyFrame(...) + { + // It is also possible to use event tables, but dynamic binding is simpler. + Bind(wxEVT_THREAD, &MyFrame::OnThreadUpdate, this); + } + ~MyFrame() { // it's better to do any thread cleanup in the OnClose() @@ -336,9 +339,7 @@ public: wxDECLARE_EVENT_TABLE(); }; - wxDEFINE_EVENT(myEVT_THREAD_UPDATE, wxThreadEvent); wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) - EVT_THREAD(wxID_ANY, myEVT_THREAD_UPDATE, MyFrame::OnThreadUpdate) EVT_CLOSE(MyFrame::OnClose) wxEND_EVENT_TABLE() @@ -362,8 +363,8 @@ public: wxThread::ExitCode MyFrame::Entry() { - // IMPORTANT: - // this function gets executed in the secondary thread context! + // VERY IMPORTANT: this function gets executed in the secondary thread context! + // Do not call any GUI function inside this function; rather use wxQueueEvent(): int offset = 0; @@ -385,11 +386,8 @@ public: offset += 1024; } - - // VERY IMPORTANT: do not call any GUI function inside this - // function; rather use wxQueueEvent(): - wxQueueEvent(this, new wxThreadEvent(wxEVT_COMMAND_MYTHREAD_UPDATE)); - // we used pointer 'this' assuming it's safe; see OnClose() + // signal to main thread that download is complete + wxQueueEvent(GetEventHandler(), new wxThreadEvent()); } // TestDestroy() returned true (which means the main thread asked us