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.
This commit is contained in:
@@ -303,12 +303,15 @@ public:
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
@code
|
@code
|
||||||
wxDECLARE_EVENT(myEVT_THREAD_UPDATE, wxThreadEvent);
|
|
||||||
|
|
||||||
class MyFrame : public wxFrame, public wxThreadHelper
|
class MyFrame : public wxFrame, public wxThreadHelper
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MyFrame(...) { ... }
|
MyFrame(...)
|
||||||
|
{
|
||||||
|
// It is also possible to use event tables, but dynamic binding is simpler.
|
||||||
|
Bind(wxEVT_THREAD, &MyFrame::OnThreadUpdate, this);
|
||||||
|
}
|
||||||
|
|
||||||
~MyFrame()
|
~MyFrame()
|
||||||
{
|
{
|
||||||
// it's better to do any thread cleanup in the OnClose()
|
// it's better to do any thread cleanup in the OnClose()
|
||||||
@@ -336,9 +339,7 @@ public:
|
|||||||
wxDECLARE_EVENT_TABLE();
|
wxDECLARE_EVENT_TABLE();
|
||||||
};
|
};
|
||||||
|
|
||||||
wxDEFINE_EVENT(myEVT_THREAD_UPDATE, wxThreadEvent);
|
|
||||||
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
||||||
EVT_THREAD(wxID_ANY, myEVT_THREAD_UPDATE, MyFrame::OnThreadUpdate)
|
|
||||||
EVT_CLOSE(MyFrame::OnClose)
|
EVT_CLOSE(MyFrame::OnClose)
|
||||||
wxEND_EVENT_TABLE()
|
wxEND_EVENT_TABLE()
|
||||||
|
|
||||||
@@ -362,8 +363,8 @@ public:
|
|||||||
|
|
||||||
wxThread::ExitCode MyFrame::Entry()
|
wxThread::ExitCode MyFrame::Entry()
|
||||||
{
|
{
|
||||||
// IMPORTANT:
|
// VERY IMPORTANT: this function gets executed in the secondary thread context!
|
||||||
// this function gets executed in the secondary thread context!
|
// Do not call any GUI function inside this function; rather use wxQueueEvent():
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
@@ -385,11 +386,8 @@ public:
|
|||||||
offset += 1024;
|
offset += 1024;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// signal to main thread that download is complete
|
||||||
// VERY IMPORTANT: do not call any GUI function inside this
|
wxQueueEvent(GetEventHandler(), new wxThreadEvent());
|
||||||
// function; rather use wxQueueEvent():
|
|
||||||
wxQueueEvent(this, new wxThreadEvent(wxEVT_COMMAND_MYTHREAD_UPDATE));
|
|
||||||
// we used pointer 'this' assuming it's safe; see OnClose()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDestroy() returned true (which means the main thread asked us
|
// TestDestroy() returned true (which means the main thread asked us
|
||||||
|
Reference in New Issue
Block a user