]> git.saurik.com Git - wxWidgets.git/blob - samples/thread/thread.cpp
link with wininet.lib under Windows to avoid linking errors in wxUSE_URL_NATIVE=...
[wxWidgets.git] / samples / thread / thread.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: thread.cpp
3 // Purpose: wxWidgets thread sample
4 // Author: Guilhem Lavaux, Vadim Zeitlin
5 // Modified by:
6 // Created: 06/16/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998-2002 wxWidgets team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #if !wxUSE_THREADS
24 #error "This sample requires thread support!"
25 #endif // wxUSE_THREADS
26
27 #include "wx/thread.h"
28 #include "wx/dynarray.h"
29 #include "wx/numdlg.h"
30
31 #include "wx/progdlg.h"
32
33 #include "../sample.xpm"
34
35 // define this to use wxExecute in the exec tests, otherwise just use system
36 #define USE_EXECUTE
37
38 #ifdef USE_EXECUTE
39 #define EXEC(cmd) wxExecute((cmd), wxEXEC_SYNC)
40 #else
41 #define EXEC(cmd) system(cmd)
42 #endif
43
44 class MyThread;
45 WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
46
47 // Define a new application type
48 class MyApp : public wxApp
49 {
50 public:
51 MyApp();
52 virtual ~MyApp(){};
53
54 virtual bool OnInit();
55
56 // critical section protects access to all of the fields below
57 wxCriticalSection m_critsect;
58
59 // all the threads currently alive - as soon as the thread terminates, it's
60 // removed from the array
61 wxArrayThread m_threads;
62
63 // semaphore used to wait for the threads to exit, see MyFrame::OnQuit()
64 wxSemaphore m_semAllDone;
65
66 // indicates that we're shutting down and all threads should exit
67 bool m_shuttingDown;
68 };
69
70 // Create a new application object
71 IMPLEMENT_APP(MyApp)
72
73 // Define a new frame type
74 class MyFrame: public wxFrame
75 {
76 public:
77 // ctor
78 MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h);
79 virtual ~MyFrame();
80
81 // this function is MT-safe, i.e. it can be called from worker threads
82 // safely without any additional locking
83 void LogThreadMessage(const wxString& text)
84 {
85 wxCriticalSectionLocker lock(m_csMessages);
86 m_messages.push_back(text);
87
88 // as we effectively log the messages from the idle event handler,
89 // ensure it's going to be called now that we have some messages to log
90 wxWakeUpIdle();
91 }
92
93 // accessors for MyWorkerThread (called in its context!)
94 bool Cancelled();
95
96 private:
97 // event handlers
98 void OnQuit(wxCommandEvent& event);
99 void OnClear(wxCommandEvent& event);
100
101 void OnStartThread(wxCommandEvent& event);
102 void OnStartThreads(wxCommandEvent& event);
103 void OnStopThread(wxCommandEvent& event);
104 void OnPauseThread(wxCommandEvent& event);
105 void OnResumeThread(wxCommandEvent& event);
106
107 void OnStartWorker(wxCommandEvent& event);
108 void OnWorkerEvent(wxCommandEvent& event);
109 void OnUpdateWorker(wxUpdateUIEvent& event);
110
111 void OnExecMain(wxCommandEvent& event);
112 void OnExecThread(wxCommandEvent& event);
113
114 void OnShowCPUs(wxCommandEvent& event);
115 void OnAbout(wxCommandEvent& event);
116
117 void OnIdle(wxIdleEvent &event);
118
119 // helper function - creates a new thread (but doesn't run it)
120 MyThread *CreateThread();
121
122 // update display in our status bar: called during idle handling
123 void UpdateThreadStatus();
124
125 // log the messages queued by LogThreadMessage()
126 void DoLogThreadMessages();
127
128
129 // just some place to put our messages in
130 wxTextCtrl *m_txtctrl;
131
132 // the array of pending messages to be displayed and the critical section
133 // protecting it
134 wxArrayString m_messages;
135 wxCriticalSection m_csMessages;
136
137 // remember the number of running threads and total number of threads
138 size_t m_nRunning,
139 m_nCount;
140
141 // the progress dialog which we show while worker thread is running
142 wxProgressDialog *m_dlgProgress;
143
144 // was the worker thread cancelled by user?
145 bool m_cancelled;
146
147 // protects m_cancelled
148 wxCriticalSection m_critsectWork;
149
150 DECLARE_EVENT_TABLE()
151 };
152
153 // ID for the menu commands
154 enum
155 {
156 THREAD_QUIT = wxID_EXIT,
157 THREAD_ABOUT = wxID_ABOUT,
158 THREAD_TEXT = 101,
159 THREAD_CLEAR,
160 THREAD_START_THREAD = 201,
161 THREAD_START_THREADS,
162 THREAD_STOP_THREAD,
163 THREAD_PAUSE_THREAD,
164 THREAD_RESUME_THREAD,
165 THREAD_START_WORKER,
166
167 THREAD_EXEC_MAIN,
168 THREAD_EXEC_THREAD,
169
170 THREAD_SHOWCPUS,
171
172 WORKER_EVENT // this one gets sent from the worker thread
173 };
174
175 // ----------------------------------------------------------------------------
176 // GUI thread
177 // ----------------------------------------------------------------------------
178
179 class MyThread : public wxThread
180 {
181 public:
182 MyThread(MyFrame *frame);
183 virtual ~MyThread();
184
185 // thread execution starts here
186 virtual void *Entry();
187
188 // write something to the text control in the main frame
189 void WriteText(const wxString& text)
190 {
191 m_frame->LogThreadMessage(text);
192 }
193
194 public:
195 unsigned m_count;
196 MyFrame *m_frame;
197 };
198
199 MyThread::MyThread(MyFrame *frame)
200 : wxThread()
201 {
202 m_count = 0;
203 m_frame = frame;
204 }
205
206 MyThread::~MyThread()
207 {
208 wxCriticalSectionLocker locker(wxGetApp().m_critsect);
209
210 wxArrayThread& threads = wxGetApp().m_threads;
211 threads.Remove(this);
212
213 if ( threads.IsEmpty() )
214 {
215 // signal the main thread that there are no more threads left if it is
216 // waiting for us
217 if ( wxGetApp().m_shuttingDown )
218 {
219 wxGetApp().m_shuttingDown = false;
220
221 wxGetApp().m_semAllDone.Post();
222 }
223 }
224 }
225
226 void *MyThread::Entry()
227 {
228 wxString text;
229
230 text.Printf(wxT("Thread 0x%lx started (priority = %u).\n"),
231 GetId(), GetPriority());
232 WriteText(text);
233 // wxLogMessage(text); -- test wxLog thread safeness
234
235 for ( m_count = 0; m_count < 10; m_count++ )
236 {
237 // check if the application is shutting down: in this case all threads
238 // should stop a.s.a.p.
239 {
240 wxCriticalSectionLocker locker(wxGetApp().m_critsect);
241 if ( wxGetApp().m_shuttingDown )
242 return NULL;
243 }
244
245 // check if just this thread was asked to exit
246 if ( TestDestroy() )
247 break;
248
249 text.Printf(wxT("[%u] Thread 0x%lx here.\n"), m_count, GetId());
250 WriteText(text);
251
252 // wxSleep() can't be called from non-GUI thread!
253 wxThread::Sleep(1000);
254 }
255
256 text.Printf(wxT("Thread 0x%lx finished.\n"), GetId());
257 WriteText(text);
258 // wxLogMessage(text); -- test wxLog thread safeness
259
260 return NULL;
261 }
262
263 // ----------------------------------------------------------------------------
264 // worker thread
265 // ----------------------------------------------------------------------------
266
267 class MyWorkerThread : public wxThread
268 {
269 public:
270 MyWorkerThread(MyFrame *frame);
271
272 // thread execution starts here
273 virtual void *Entry();
274
275 // called when the thread exits - whether it terminates normally or is
276 // stopped with Delete() (but not when it is Kill()ed!)
277 virtual void OnExit();
278
279 public:
280 MyFrame *m_frame;
281 unsigned m_count;
282 };
283
284 MyWorkerThread::MyWorkerThread(MyFrame *frame)
285 : wxThread()
286 {
287 m_frame = frame;
288 m_count = 0;
289 }
290
291 void MyWorkerThread::OnExit()
292 {
293 }
294
295 void *MyWorkerThread::Entry()
296 {
297 for ( m_count = 0; !m_frame->Cancelled() && (m_count < 100); m_count++ )
298 {
299 // check if we were asked to exit
300 if ( TestDestroy() )
301 break;
302
303 // create any type of command event here
304 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
305 event.SetInt( m_count );
306
307 // send in a thread-safe way
308 wxQueueEvent( m_frame, new wxCommandEvent(event) );
309
310 wxMilliSleep(200);
311 }
312
313 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
314 event.SetInt(-1); // that's all
315 wxQueueEvent( m_frame, new wxCommandEvent(event) );
316
317 return NULL;
318 }
319
320 // ----------------------------------------------------------------------------
321 // a thread which simply calls wxExecute
322 // ----------------------------------------------------------------------------
323
324 class MyExecThread : public wxThread
325 {
326 public:
327 MyExecThread(const wxChar *command) : wxThread(wxTHREAD_JOINABLE),
328 m_command(command)
329 {
330 Create();
331 }
332
333 virtual ExitCode Entry()
334 {
335 return wxUIntToPtr(EXEC(m_command));
336 }
337
338 private:
339 wxString m_command;
340 };
341
342 // ----------------------------------------------------------------------------
343 // implementation
344 // ----------------------------------------------------------------------------
345
346 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
347 EVT_MENU(THREAD_QUIT, MyFrame::OnQuit)
348 EVT_MENU(THREAD_CLEAR, MyFrame::OnClear)
349 EVT_MENU(THREAD_START_THREAD, MyFrame::OnStartThread)
350 EVT_MENU(THREAD_START_THREADS, MyFrame::OnStartThreads)
351 EVT_MENU(THREAD_STOP_THREAD, MyFrame::OnStopThread)
352 EVT_MENU(THREAD_PAUSE_THREAD, MyFrame::OnPauseThread)
353 EVT_MENU(THREAD_RESUME_THREAD, MyFrame::OnResumeThread)
354
355 EVT_MENU(THREAD_EXEC_MAIN, MyFrame::OnExecMain)
356 EVT_MENU(THREAD_EXEC_THREAD, MyFrame::OnExecThread)
357
358 EVT_MENU(THREAD_SHOWCPUS, MyFrame::OnShowCPUs)
359 EVT_MENU(THREAD_ABOUT, MyFrame::OnAbout)
360
361 EVT_UPDATE_UI(THREAD_START_WORKER, MyFrame::OnUpdateWorker)
362 EVT_MENU(THREAD_START_WORKER, MyFrame::OnStartWorker)
363 EVT_MENU(WORKER_EVENT, MyFrame::OnWorkerEvent)
364
365 EVT_IDLE(MyFrame::OnIdle)
366 END_EVENT_TABLE()
367
368 MyApp::MyApp()
369 {
370 m_shuttingDown = false;
371 }
372
373 // `Main program' equivalent, creating windows and returning main app frame
374 bool MyApp::OnInit()
375 {
376 if ( !wxApp::OnInit() )
377 return false;
378
379 // uncomment this to get some debugging messages from the trace code
380 // on the console (or just set WXTRACE env variable to include "thread")
381 //wxLog::AddTraceMask("thread");
382
383 // Create the main frame window
384 MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWidgets threads sample"),
385 50, 50, 450, 340);
386
387 // Make a menubar
388 wxMenuBar *menuBar = new wxMenuBar;
389
390 wxMenu *menuFile = new wxMenu;
391 menuFile->Append(THREAD_CLEAR, _T("&Clear log\tCtrl-L"));
392 menuFile->AppendSeparator();
393 menuFile->Append(THREAD_QUIT, _T("E&xit\tAlt-X"));
394 menuBar->Append(menuFile, _T("&File"));
395
396 wxMenu *menuThread = new wxMenu;
397 menuThread->Append(THREAD_START_THREAD, _T("&Start a new thread\tCtrl-N"));
398 menuThread->Append(THREAD_START_THREADS, _T("Start &many threads at once"));
399 menuThread->Append(THREAD_STOP_THREAD, _T("S&top a running thread\tCtrl-S"));
400 menuThread->AppendSeparator();
401 menuThread->Append(THREAD_PAUSE_THREAD, _T("&Pause a running thread\tCtrl-P"));
402 menuThread->Append(THREAD_RESUME_THREAD, _T("&Resume suspended thread\tCtrl-R"));
403 menuThread->AppendSeparator();
404 menuThread->Append(THREAD_START_WORKER, _T("Start &worker thread\tCtrl-W"));
405 menuBar->Append(menuThread, _T("&Thread"));
406
407 wxMenu *menuExec = new wxMenu;
408 menuExec->Append(THREAD_EXEC_MAIN, _T("&Launch a program from main thread\tF5"));
409 menuExec->Append(THREAD_EXEC_THREAD, _T("L&aunch a program from a thread\tCtrl-F5"));
410 menuBar->Append(menuExec, _T("&Execute"));
411
412 wxMenu *menuHelp = new wxMenu;
413 menuHelp->Append(THREAD_SHOWCPUS, _T("&Show CPU count"));
414 menuHelp->AppendSeparator();
415 menuHelp->Append(THREAD_ABOUT, _T("&About..."));
416 menuBar->Append(menuHelp, _T("&Help"));
417
418 frame->SetMenuBar(menuBar);
419
420 // Show the frame
421 frame->Show(true);
422
423 SetTopWindow(frame);
424
425 return true;
426 }
427
428 // My frame constructor
429 MyFrame::MyFrame(wxFrame *frame, const wxString& title,
430 int x, int y, int w, int h)
431 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
432 {
433 SetIcon(wxIcon(sample_xpm));
434
435 m_nRunning = m_nCount = 0;
436
437 m_dlgProgress = (wxProgressDialog *)NULL;
438
439 #if wxUSE_STATUSBAR
440 CreateStatusBar(2);
441 #endif // wxUSE_STATUSBAR
442
443 m_txtctrl = new wxTextCtrl(this, wxID_ANY, _T(""), wxPoint(0, 0), wxSize(0, 0),
444 wxTE_MULTILINE | wxTE_READONLY);
445
446 }
447
448 MyFrame::~MyFrame()
449 {
450 // NB: although the OS will terminate all the threads anyhow when the main
451 // one exits, it's good practice to do it ourselves -- even if it's not
452 // completely trivial in this example
453
454 // tell all the threads to terminate: note that they can't terminate while
455 // we're deleting them because they will block in their OnExit() -- this is
456 // important as otherwise we might access invalid array elements
457
458 {
459 wxCriticalSectionLocker locker(wxGetApp().m_critsect);
460
461 // check if we have any threads running first
462 const wxArrayThread& threads = wxGetApp().m_threads;
463 size_t count = threads.GetCount();
464
465 if ( !count )
466 return;
467
468 // set the flag indicating that all threads should exit
469 wxGetApp().m_shuttingDown = true;
470 }
471
472 // now wait for them to really terminate
473 wxGetApp().m_semAllDone.Wait();
474 }
475
476 MyThread *MyFrame::CreateThread()
477 {
478 MyThread *thread = new MyThread(this);
479
480 if ( thread->Create() != wxTHREAD_NO_ERROR )
481 {
482 wxLogError(wxT("Can't create thread!"));
483 }
484
485 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
486 wxGetApp().m_threads.Add(thread);
487
488 return thread;
489 }
490
491 void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) )
492 {
493 static long s_num;
494
495 s_num = wxGetNumberFromUser(_T("How many threads to start: "), _T(""),
496 _T("wxThread sample"), s_num, 1, 10000, this);
497 if ( s_num == -1 )
498 {
499 s_num = 10;
500
501 return;
502 }
503
504 unsigned count = unsigned(s_num), n;
505
506 wxArrayThread threads;
507
508 // first create them all...
509 for ( n = 0; n < count; n++ )
510 {
511 wxThread *thr = CreateThread();
512
513 // we want to show the effect of SetPriority(): the first thread will
514 // have the lowest priority, the second - the highest, all the rest
515 // the normal one
516 if ( n == 0 )
517 thr->SetPriority(WXTHREAD_MIN_PRIORITY);
518 else if ( n == 1 )
519 thr->SetPriority(WXTHREAD_MAX_PRIORITY);
520 else
521 thr->SetPriority(WXTHREAD_DEFAULT_PRIORITY);
522
523 threads.Add(thr);
524 }
525
526 #if wxUSE_STATUSBAR
527 wxString msg;
528 msg.Printf(wxT("%d new threads created."), count);
529 SetStatusText(msg, 1);
530 #endif // wxUSE_STATUSBAR
531
532 // ...and then start them
533 for ( n = 0; n < count; n++ )
534 {
535 threads[n]->Run();
536 }
537 }
538
539 void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
540 {
541 MyThread *thread = CreateThread();
542
543 if ( thread->Run() != wxTHREAD_NO_ERROR )
544 {
545 wxLogError(wxT("Can't start thread!"));
546 }
547
548 #if wxUSE_STATUSBAR
549 SetStatusText(_T("New thread started."), 1);
550 #endif // wxUSE_STATUSBAR
551 }
552
553 void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
554 {
555 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
556
557 // stop the last thread
558 if ( wxGetApp().m_threads.IsEmpty() )
559 {
560 wxLogError(wxT("No thread to stop!"));
561 }
562 else
563 {
564 wxGetApp().m_threads.Last()->Delete();
565
566 #if wxUSE_STATUSBAR
567 SetStatusText(_T("Last thread stopped."), 1);
568 #endif // wxUSE_STATUSBAR
569 }
570 }
571
572 void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
573 {
574 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
575
576 // resume first suspended thread
577 size_t n = 0, count = wxGetApp().m_threads.Count();
578 while ( n < count && !wxGetApp().m_threads[n]->IsPaused() )
579 n++;
580
581 if ( n == count )
582 {
583 wxLogError(wxT("No thread to resume!"));
584 }
585 else
586 {
587 wxGetApp().m_threads[n]->Resume();
588
589 #if wxUSE_STATUSBAR
590 SetStatusText(_T("Thread resumed."), 1);
591 #endif // wxUSE_STATUSBAR
592 }
593 }
594
595 void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
596 {
597 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
598
599 // pause last running thread
600 int n = wxGetApp().m_threads.Count() - 1;
601 while ( n >= 0 && !wxGetApp().m_threads[n]->IsRunning() )
602 n--;
603
604 if ( n < 0 )
605 {
606 wxLogError(wxT("No thread to pause!"));
607 }
608 else
609 {
610 wxGetApp().m_threads[n]->Pause();
611
612 #if wxUSE_STATUSBAR
613 SetStatusText(_T("Thread paused."), 1);
614 #endif // wxUSE_STATUSBAR
615 }
616 }
617
618 void MyFrame::OnIdle(wxIdleEvent& event)
619 {
620 DoLogThreadMessages();
621
622 UpdateThreadStatus();
623
624 event.Skip();
625 }
626
627 void MyFrame::DoLogThreadMessages()
628 {
629 wxCriticalSectionLocker lock(m_csMessages);
630
631 const size_t count = m_messages.size();
632 for ( size_t n = 0; n < count; n++ )
633 {
634 m_txtctrl->AppendText(m_messages[n]);
635 }
636
637 m_messages.clear();
638 }
639
640 void MyFrame::UpdateThreadStatus()
641 {
642 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
643
644 // update the counts of running/total threads
645 size_t nRunning = 0,
646 nCount = wxGetApp().m_threads.Count();
647 for ( size_t n = 0; n < nCount; n++ )
648 {
649 if ( wxGetApp().m_threads[n]->IsRunning() )
650 nRunning++;
651 }
652
653 if ( nCount != m_nCount || nRunning != m_nRunning )
654 {
655 m_nRunning = nRunning;
656 m_nCount = nCount;
657
658 wxLogStatus(this, wxT("%u threads total, %u running."), unsigned(nCount), unsigned(nRunning));
659 }
660 //else: avoid flicker - don't print anything
661 }
662
663 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
664 {
665 Close(true);
666 }
667
668 void MyFrame::OnExecMain(wxCommandEvent& WXUNUSED(event))
669 {
670 wxLogMessage(wxT("The exit code from the main program is %ld"),
671 EXEC(_T("/bin/echo \"main program\"")));
672 }
673
674 void MyFrame::OnExecThread(wxCommandEvent& WXUNUSED(event))
675 {
676 MyExecThread thread(wxT("/bin/echo \"child thread\""));
677 thread.Run();
678
679 wxLogMessage(wxT("The exit code from a child thread is %ld"),
680 (long)wxPtrToUInt(thread.Wait()));
681 }
682
683 void MyFrame::OnShowCPUs(wxCommandEvent& WXUNUSED(event))
684 {
685 wxString msg;
686
687 int nCPUs = wxThread::GetCPUCount();
688 switch ( nCPUs )
689 {
690 case -1:
691 msg = _T("Unknown number of CPUs");
692 break;
693
694 case 0:
695 msg = _T("WARNING: you're running without any CPUs!");
696 break;
697
698 case 1:
699 msg = _T("This system only has one CPU.");
700 break;
701
702 default:
703 msg.Printf(wxT("This system has %d CPUs"), nCPUs);
704 }
705
706 wxLogMessage(msg);
707 }
708
709 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
710 {
711 wxMessageDialog dialog(this,
712 _T("wxWidgets multithreaded application sample\n")
713 _T("(c) 1998 Julian Smart, Guilhem Lavaux\n")
714 _T("(c) 1999 Vadim Zeitlin\n")
715 _T("(c) 2000 Robert Roebling"),
716 _T("About wxThread sample"),
717 wxOK | wxICON_INFORMATION);
718
719 dialog.ShowModal();
720 }
721
722 void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
723 {
724 m_txtctrl->Clear();
725 }
726
727 void MyFrame::OnUpdateWorker(wxUpdateUIEvent& event)
728 {
729 event.Enable( m_dlgProgress == NULL );
730 }
731
732 void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event))
733 {
734 MyWorkerThread *thread = new MyWorkerThread(this);
735
736 if ( thread->Create() != wxTHREAD_NO_ERROR )
737 {
738 wxLogError(wxT("Can't create thread!"));
739 return;
740 }
741
742 m_dlgProgress = new wxProgressDialog
743 (
744 _T("Progress dialog"),
745 _T("Wait until the thread terminates or press [Cancel]"),
746 100,
747 this,
748 wxPD_CAN_ABORT |
749 wxPD_APP_MODAL |
750 wxPD_ELAPSED_TIME |
751 wxPD_ESTIMATED_TIME |
752 wxPD_REMAINING_TIME
753 );
754
755 // thread is not running yet, no need for crit sect
756 m_cancelled = false;
757
758 thread->Run();
759 }
760
761 void MyFrame::OnWorkerEvent(wxCommandEvent& event)
762 {
763 int n = event.GetInt();
764 if ( n == -1 )
765 {
766 m_dlgProgress->Destroy();
767 m_dlgProgress = (wxProgressDialog *)NULL;
768
769 // the dialog is aborted because the event came from another thread, so
770 // we may need to wake up the main event loop for the dialog to be
771 // really closed
772 wxWakeUpIdle();
773 }
774 else
775 {
776 if ( !m_dlgProgress->Update(n) )
777 {
778 wxCriticalSectionLocker lock(m_critsectWork);
779
780 m_cancelled = true;
781 }
782 }
783 }
784
785 bool MyFrame::Cancelled()
786 {
787 wxCriticalSectionLocker lock(m_critsectWork);
788
789 return m_cancelled;
790 }