+
+void MyFrame::OnUpdateWorker(wxUpdateUIEvent& event)
+{
+ event.Enable( m_dlgProgress == NULL );
+}
+
+void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event))
+{
+ MyWorkerThread *thread = new MyWorkerThread(this);
+
+ if ( thread->Create() != wxTHREAD_NO_ERROR )
+ {
+ wxLogError("Can't create thread!");
+ }
+
+ m_dlgProgress = new wxProgressDialog
+ (
+ "Progress dialog",
+ "Wait until the thread terminates or press [Cancel]",
+ 100,
+ this,
+ wxPD_CAN_ABORT |
+ wxPD_APP_MODAL |
+ wxPD_ELAPSED_TIME |
+ wxPD_ESTIMATED_TIME |
+ wxPD_REMAINING_TIME
+ );
+
+ // thread is not running yet, no need for crit sect
+ m_cancelled = FALSE;
+
+ thread->Run();
+}
+
+void MyFrame::OnWorkerEvent(wxCommandEvent& event)
+{
+#if 0
+ WriteText( "Got message from worker thread: " );
+ WriteText( event.GetString() );
+ WriteText( "\n" );
+#else
+ int n = event.GetInt();
+ if ( n == -1 )
+ {
+ m_dlgProgress->Destroy();
+ m_dlgProgress = (wxProgressDialog *)NULL;
+
+ // the dialog is aborted because the event came from another thread, so
+ // we may need to wake up the main event loop for the dialog to be
+ // really closed
+ wxWakeUpIdle();
+ }
+ else
+ {
+ if ( !m_dlgProgress->Update(n) )
+ {
+ wxCriticalSectionLocker lock(m_critsectWork);
+
+ m_cancelled = TRUE;
+ }
+ }
+#endif
+}
+
+bool MyFrame::Cancelled()
+{
+ wxCriticalSectionLocker lock(m_critsectWork);
+
+ return m_cancelled;
+}