+
+
+// ----------------------------------------------------------------------------
+// MyThread
+// ----------------------------------------------------------------------------
+
+MyThread::MyThread(MyFrame *frame)
+ : wxThread()
+{
+ m_count = 0;
+ m_frame = frame;
+}
+
+MyThread::~MyThread()
+{
+ wxCriticalSectionLocker locker(wxGetApp().m_critsect);
+
+ wxArrayThread& threads = wxGetApp().m_threads;
+ threads.Remove(this);
+
+ if ( threads.IsEmpty() )
+ {
+ // signal the main thread that there are no more threads left if it is
+ // waiting for us
+ if ( wxGetApp().m_shuttingDown )
+ {
+ wxGetApp().m_shuttingDown = false;
+
+ wxGetApp().m_semAllDone.Post();
+ }
+ }
+}
+
+void *MyThread::Entry()
+{
+ wxString text;
+
+ text.Printf(wxT("Thread 0x%lx started (priority = %u).\n"),
+ GetId(), GetPriority());
+ WriteText(text);
+ // wxLogMessage(text); -- test wxLog thread safeness
+
+ for ( m_count = 0; m_count < 10; m_count++ )
+ {
+ // check if the application is shutting down: in this case all threads
+ // should stop a.s.a.p.
+ {
+ wxCriticalSectionLocker locker(wxGetApp().m_critsect);
+ if ( wxGetApp().m_shuttingDown )
+ return NULL;
+ }
+
+ // check if just this thread was asked to exit
+ if ( TestDestroy() )
+ break;
+
+ text.Printf(wxT("[%u] Thread 0x%lx here.\n"), m_count, GetId());
+ WriteText(text);
+
+ // wxSleep() can't be called from non-GUI thread!
+ wxThread::Sleep(1000);
+ }
+
+ text.Printf(wxT("Thread 0x%lx finished.\n"), GetId());
+ WriteText(text);
+ // wxLogMessage(text); -- test wxLog thread safeness
+
+ return NULL;
+}
+
+
+// ----------------------------------------------------------------------------
+// MyWorkerThread
+// ----------------------------------------------------------------------------
+
+MyWorkerThread::MyWorkerThread(MyFrame *frame)
+ : wxThread()
+{
+ m_frame = frame;
+ m_count = 0;
+}
+
+void MyWorkerThread::OnExit()
+{
+}
+
+// define this symbol to 1 to test if the YieldFor() call in the wxProgressDialog::Update
+// function provokes a race condition in which the second wxThreadEvent posted by
+// MyWorkerThread::Entry is processed by the YieldFor() call of wxProgressDialog::Update
+// and results in the destruction of the progress dialog itself, resulting in a crash later.
+#define TEST_YIELD_RACE_CONDITION 0
+
+void *MyWorkerThread::Entry()
+{
+#if TEST_YIELD_RACE_CONDITION
+ if ( TestDestroy() )
+ return NULL;
+
+ wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT );
+
+ event.SetInt( 50 );
+ wxQueueEvent( m_frame, event.Clone() );
+
+ event.SetInt(-1);
+ wxQueueEvent( m_frame, event.Clone() );
+#else
+ for ( m_count = 0; !m_frame->Cancelled() && (m_count < 100); m_count++ )
+ {
+ // check if we were asked to exit
+ if ( TestDestroy() )
+ break;
+
+ // create any type of command event here
+ wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT );
+ event.SetInt( m_count );
+
+ // send in a thread-safe way
+ wxQueueEvent( m_frame, event.Clone() );
+
+ wxMilliSleep(200);
+ }
+
+ wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT );
+ event.SetInt(-1); // that's all
+ wxQueueEvent( m_frame, event.Clone() );
+#endif
+
+ return NULL;
+}
+