1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets thread sample
4 // Author: Guilhem Lavaux, Vadim Zeitlin
8 // Copyright: (c) 1998-2002 wxWidgets team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
32 #error "This sample requires thread support!"
33 #endif // wxUSE_THREADS
35 #include "wx/thread.h"
36 #include "wx/dynarray.h"
37 #include "wx/numdlg.h"
38 #include "wx/progdlg.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // define this to use wxExecute in the exec tests, otherwise just use system
54 #define EXEC(cmd) wxExecute((cmd), wxEXEC_SYNC)
56 #define EXEC(cmd) system(cmd)
60 WX_DEFINE_ARRAY_PTR(wxThread
*, wxArrayThread
);
62 // ----------------------------------------------------------------------------
63 // the application object
64 // ----------------------------------------------------------------------------
66 class MyApp
: public wxApp
72 virtual bool OnInit();
74 // critical section protects access to all of the fields below
75 wxCriticalSection m_critsect
;
77 // all the threads currently alive - as soon as the thread terminates, it's
78 // removed from the array
79 wxArrayThread m_threads
;
81 // semaphore used to wait for the threads to exit, see MyFrame::OnQuit()
82 wxSemaphore m_semAllDone
;
84 // indicates that we're shutting down and all threads should exit
88 // ----------------------------------------------------------------------------
89 // the main application frame
90 // ----------------------------------------------------------------------------
92 class MyFrame
: public wxFrame
96 MyFrame(wxFrame
*frame
, const wxString
& title
, int x
, int y
, int w
, int h
);
99 // this function is MT-safe, i.e. it can be called from worker threads
100 // safely without any additional locking
101 void LogThreadMessage(const wxString
& text
)
103 wxCriticalSectionLocker
lock(m_csMessages
);
104 m_messages
.push_back(text
);
106 // as we effectively log the messages from the idle event handler,
107 // ensure it's going to be called now that we have some messages to log
111 // accessors for MyWorkerThread (called in its context!)
118 void OnQuit(wxCommandEvent
& event
);
119 void OnClear(wxCommandEvent
& event
);
121 void OnStartThread(wxCommandEvent
& event
);
122 void OnStartThreads(wxCommandEvent
& event
);
123 void OnStopThread(wxCommandEvent
& event
);
124 void OnPauseThread(wxCommandEvent
& event
);
125 void OnResumeThread(wxCommandEvent
& event
);
127 void OnStartWorker(wxCommandEvent
& event
);
128 void OnExecMain(wxCommandEvent
& event
);
129 void OnStartGUIThread(wxCommandEvent
& event
);
131 void OnShowCPUs(wxCommandEvent
& event
);
132 void OnAbout(wxCommandEvent
& event
);
134 void OnIdle(wxIdleEvent
&event
);
135 void OnWorkerEvent(wxThreadEvent
& event
);
136 void OnUpdateWorker(wxUpdateUIEvent
& event
);
139 // thread helper functions
140 // -----------------------
142 // helper function - creates a new thread (but doesn't run it)
143 MyThread
*CreateThread();
145 // update display in our status bar: called during idle handling
146 void UpdateThreadStatus();
148 // log the messages queued by LogThreadMessage()
149 void DoLogThreadMessages();
152 // internal variables
153 // ------------------
155 // just some place to put our messages in
156 wxTextCtrl
*m_txtctrl
;
158 // the array of pending messages to be displayed and the critical section
160 wxArrayString m_messages
;
161 wxCriticalSection m_csMessages
;
163 // remember the number of running threads and total number of threads
167 // the progress dialog which we show while worker thread is running
168 wxProgressDialog
*m_dlgProgress
;
170 // was the worker thread cancelled by user?
172 wxCriticalSection m_csCancelled
; // protects m_cancelled
174 DECLARE_EVENT_TABLE()
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 // ID for the menu commands
184 THREAD_QUIT
= wxID_EXIT
,
185 THREAD_ABOUT
= wxID_ABOUT
,
188 THREAD_START_THREAD
= 201,
189 THREAD_START_THREADS
,
192 THREAD_RESUME_THREAD
,
196 THREAD_START_GUI_THREAD
,
200 WORKER_EVENT
= wxID_HIGHEST
+1, // this one gets sent from MyWorkerThread
201 GUITHREAD_EVENT
// this one gets sent from MyGUIThread
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 class MyThread
: public wxThread
211 MyThread(MyFrame
*frame
);
214 // thread execution starts here
215 virtual void *Entry();
217 // write something to the text control in the main frame
218 void WriteText(const wxString
& text
)
220 m_frame
->LogThreadMessage(text
);
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 class MyWorkerThread
: public wxThread
235 MyWorkerThread(MyFrame
*frame
);
237 // thread execution starts here
238 virtual void *Entry();
240 // called when the thread exits - whether it terminates normally or is
241 // stopped with Delete() (but not when it is Kill()ed!)
242 virtual void OnExit();
249 // ----------------------------------------------------------------------------
250 // a thread which executes GUI calls using wxMutexGuiEnter/Leave
251 // ----------------------------------------------------------------------------
253 #define GUITHREAD_BMP_SIZE 300
254 #define GUITHREAD_NUM_UPDATES 50
257 class MyGUIThread
: public wxThread
260 MyGUIThread(MyImageDialog
*dlg
) : wxThread(wxTHREAD_JOINABLE
)
265 virtual ExitCode
Entry();
268 MyImageDialog
*m_dlg
;
271 // ----------------------------------------------------------------------------
272 // an helper dialog used by MyFrame::OnStartGUIThread
273 // ----------------------------------------------------------------------------
275 class MyImageDialog
: public wxDialog
279 MyImageDialog(wxFrame
*frame
);
282 // stuff used by MyGUIThread:
283 wxBitmap m_bmp
; // the bitmap drawn by MyGUIThread
284 wxCriticalSection m_csBmp
; // protects m_bmp
287 void OnGUIThreadEvent(wxThreadEvent
& event
);
288 void OnPaint(wxPaintEvent
&);
290 MyGUIThread m_thread
;
291 int m_nCurrentProgress
;
293 DECLARE_EVENT_TABLE()
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
301 // the application class
302 // ----------------------------------------------------------------------------
304 // Create a new application object
309 m_shuttingDown
= false;
312 // `Main program' equivalent, creating windows and returning main app frame
315 if ( !wxApp::OnInit() )
318 // uncomment this to get some debugging messages from the trace code
319 // on the console (or just set WXTRACE env variable to include "thread")
320 wxLog::AddTraceMask("thread");
322 // Create the main frame window
323 MyFrame
*frame
= new MyFrame((wxFrame
*)NULL
, _T("wxWidgets threads sample"),
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
338 EVT_MENU(THREAD_QUIT
, MyFrame::OnQuit
)
339 EVT_MENU(THREAD_CLEAR
, MyFrame::OnClear
)
340 EVT_MENU(THREAD_START_THREAD
, MyFrame::OnStartThread
)
341 EVT_MENU(THREAD_START_THREADS
, MyFrame::OnStartThreads
)
342 EVT_MENU(THREAD_STOP_THREAD
, MyFrame::OnStopThread
)
343 EVT_MENU(THREAD_PAUSE_THREAD
, MyFrame::OnPauseThread
)
344 EVT_MENU(THREAD_RESUME_THREAD
, MyFrame::OnResumeThread
)
346 EVT_MENU(THREAD_START_WORKER
, MyFrame::OnStartWorker
)
347 EVT_MENU(THREAD_EXEC_MAIN
, MyFrame::OnExecMain
)
348 EVT_MENU(THREAD_START_GUI_THREAD
, MyFrame::OnStartGUIThread
)
350 EVT_MENU(THREAD_SHOWCPUS
, MyFrame::OnShowCPUs
)
351 EVT_MENU(THREAD_ABOUT
, MyFrame::OnAbout
)
353 EVT_UPDATE_UI(THREAD_START_WORKER
, MyFrame::OnUpdateWorker
)
354 EVT_THREAD(WORKER_EVENT
, MyFrame::OnWorkerEvent
)
355 EVT_IDLE(MyFrame::OnIdle
)
358 // My frame constructor
359 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
,
360 int x
, int y
, int w
, int h
)
361 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
363 SetIcon(wxIcon(sample_xpm
));
366 wxMenuBar
*menuBar
= new wxMenuBar
;
368 wxMenu
*menuFile
= new wxMenu
;
369 menuFile
->Append(THREAD_CLEAR
, _T("&Clear log\tCtrl-L"));
370 menuFile
->AppendSeparator();
371 menuFile
->Append(THREAD_QUIT
, _T("E&xit\tAlt-X"));
372 menuBar
->Append(menuFile
, _T("&File"));
374 wxMenu
*menuThread
= new wxMenu
;
375 menuThread
->Append(THREAD_START_THREAD
, _T("&Start a new thread\tCtrl-N"));
376 menuThread
->Append(THREAD_START_THREADS
, _T("Start &many threads at once"));
377 menuThread
->Append(THREAD_STOP_THREAD
, _T("S&top the last spawned thread\tCtrl-S"));
378 menuThread
->AppendSeparator();
379 menuThread
->Append(THREAD_PAUSE_THREAD
, _T("&Pause the last spawned running thread\tCtrl-P"));
380 menuThread
->Append(THREAD_RESUME_THREAD
, _T("&Resume the first suspended thread\tCtrl-R"));
381 menuThread
->AppendSeparator();
382 menuThread
->Append(THREAD_START_WORKER
, _T("Start a &worker thread\tCtrl-W"));
383 menuThread
->Append(THREAD_EXEC_MAIN
, _T("&Launch a program from main thread\tF5"));
384 menuThread
->Append(THREAD_START_GUI_THREAD
, _T("Launch a &GUI thread\tF6"));
385 menuBar
->Append(menuThread
, _T("&Thread"));
387 wxMenu
*menuHelp
= new wxMenu
;
388 menuHelp
->Append(THREAD_SHOWCPUS
, _T("&Show CPU count"));
389 menuHelp
->AppendSeparator();
390 menuHelp
->Append(THREAD_ABOUT
, _T("&About..."));
391 menuBar
->Append(menuHelp
, _T("&Help"));
395 m_nRunning
= m_nCount
= 0;
397 m_dlgProgress
= (wxProgressDialog
*)NULL
;
401 #endif // wxUSE_STATUSBAR
403 m_txtctrl
= new wxTextCtrl(this, wxID_ANY
, _T(""), wxPoint(0, 0), wxSize(0, 0),
404 wxTE_MULTILINE
| wxTE_READONLY
);
409 // NB: although the OS will terminate all the threads anyhow when the main
410 // one exits, it's good practice to do it ourselves -- even if it's not
411 // completely trivial in this example
413 // tell all the threads to terminate: note that they can't terminate while
414 // we're deleting them because they will block in their OnExit() -- this is
415 // important as otherwise we might access invalid array elements
418 wxCriticalSectionLocker
locker(wxGetApp().m_critsect
);
420 // check if we have any threads running first
421 const wxArrayThread
& threads
= wxGetApp().m_threads
;
422 size_t count
= threads
.GetCount();
427 // set the flag indicating that all threads should exit
428 wxGetApp().m_shuttingDown
= true;
431 // now wait for them to really terminate
432 wxGetApp().m_semAllDone
.Wait();
435 MyThread
*MyFrame::CreateThread()
437 MyThread
*thread
= new MyThread(this);
439 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
441 wxLogError(wxT("Can't create thread!"));
444 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
445 wxGetApp().m_threads
.Add(thread
);
450 void MyFrame::DoLogThreadMessages()
452 wxCriticalSectionLocker
lock(m_csMessages
);
454 const size_t count
= m_messages
.size();
455 for ( size_t n
= 0; n
< count
; n
++ )
457 m_txtctrl
->AppendText(m_messages
[n
]);
463 void MyFrame::UpdateThreadStatus()
465 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
467 // update the counts of running/total threads
469 nCount
= wxGetApp().m_threads
.Count();
470 for ( size_t n
= 0; n
< nCount
; n
++ )
472 if ( wxGetApp().m_threads
[n
]->IsRunning() )
476 if ( nCount
!= m_nCount
|| nRunning
!= m_nRunning
)
478 m_nRunning
= nRunning
;
481 wxLogStatus(this, wxT("%u threads total, %u running."), unsigned(nCount
), unsigned(nRunning
));
483 //else: avoid flicker - don't print anything
486 bool MyFrame::Cancelled()
488 wxCriticalSectionLocker
lock(m_csCancelled
);
493 // ----------------------------------------------------------------------------
494 // MyFrame - event handlers
495 // ----------------------------------------------------------------------------
497 void MyFrame::OnStartThreads(wxCommandEvent
& WXUNUSED(event
) )
501 s_num
= wxGetNumberFromUser(_T("How many threads to start: "), _T(""),
502 _T("wxThread sample"), s_num
, 1, 10000, this);
510 unsigned count
= unsigned(s_num
), n
;
512 wxArrayThread threads
;
514 // first create them all...
515 for ( n
= 0; n
< count
; n
++ )
517 wxThread
*thr
= CreateThread();
519 // we want to show the effect of SetPriority(): the first thread will
520 // have the lowest priority, the second - the highest, all the rest
523 thr
->SetPriority(WXTHREAD_MIN_PRIORITY
);
525 thr
->SetPriority(WXTHREAD_MAX_PRIORITY
);
527 thr
->SetPriority(WXTHREAD_DEFAULT_PRIORITY
);
534 msg
.Printf(wxT("%d new threads created."), count
);
535 SetStatusText(msg
, 1);
536 #endif // wxUSE_STATUSBAR
538 // ...and then start them
539 for ( n
= 0; n
< count
; n
++ )
545 void MyFrame::OnStartThread(wxCommandEvent
& WXUNUSED(event
) )
547 MyThread
*thread
= CreateThread();
549 if ( thread
->Run() != wxTHREAD_NO_ERROR
)
551 wxLogError(wxT("Can't start thread!"));
555 SetStatusText(_T("New thread started."), 1);
556 #endif // wxUSE_STATUSBAR
559 void MyFrame::OnStopThread(wxCommandEvent
& WXUNUSED(event
) )
561 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
563 // stop the last thread
564 if ( wxGetApp().m_threads
.IsEmpty() )
566 wxLogError(wxT("No thread to stop!"));
570 wxGetApp().m_threads
.Last()->Delete();
573 SetStatusText(_T("Last thread stopped."), 1);
574 #endif // wxUSE_STATUSBAR
578 void MyFrame::OnResumeThread(wxCommandEvent
& WXUNUSED(event
) )
580 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
582 // resume first suspended thread
583 size_t n
= 0, count
= wxGetApp().m_threads
.Count();
584 while ( n
< count
&& !wxGetApp().m_threads
[n
]->IsPaused() )
589 wxLogError(wxT("No thread to resume!"));
593 wxGetApp().m_threads
[n
]->Resume();
596 SetStatusText(_T("Thread resumed."), 1);
597 #endif // wxUSE_STATUSBAR
601 void MyFrame::OnPauseThread(wxCommandEvent
& WXUNUSED(event
) )
603 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
605 // pause last running thread
606 int n
= wxGetApp().m_threads
.Count() - 1;
607 while ( n
>= 0 && !wxGetApp().m_threads
[n
]->IsRunning() )
612 wxLogError(wxT("No thread to pause!"));
616 wxGetApp().m_threads
[n
]->Pause();
619 SetStatusText(_T("Thread paused."), 1);
620 #endif // wxUSE_STATUSBAR
624 void MyFrame::OnIdle(wxIdleEvent
& event
)
626 DoLogThreadMessages();
628 UpdateThreadStatus();
633 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
638 void MyFrame::OnExecMain(wxCommandEvent
& WXUNUSED(event
))
640 wxString cmd
= wxGetTextFromUser("Please enter the command to execute",
645 "/bin/echo \"Message from another process\"",
649 return; // user clicked cancel
651 wxLogMessage(wxT("The exit code from the main program is %ld"),
655 void MyFrame::OnShowCPUs(wxCommandEvent
& WXUNUSED(event
))
659 int nCPUs
= wxThread::GetCPUCount();
663 msg
= _T("Unknown number of CPUs");
667 msg
= _T("WARNING: you're running without any CPUs!");
671 msg
= _T("This system only has one CPU.");
675 msg
.Printf(wxT("This system has %d CPUs"), nCPUs
);
681 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
683 wxMessageDialog
dialog(this,
684 _T("wxWidgets multithreaded application sample\n")
685 _T("(c) 1998 Julian Smart, Guilhem Lavaux\n")
686 _T("(c) 1999 Vadim Zeitlin\n")
687 _T("(c) 2000 Robert Roebling"),
688 _T("About wxThread sample"),
689 wxOK
| wxICON_INFORMATION
);
694 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
699 void MyFrame::OnUpdateWorker(wxUpdateUIEvent
& event
)
701 event
.Enable( m_dlgProgress
== NULL
);
704 void MyFrame::OnStartWorker(wxCommandEvent
& WXUNUSED(event
))
706 MyWorkerThread
*thread
= new MyWorkerThread(this);
708 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
710 wxLogError(wxT("Can't create thread!"));
714 m_dlgProgress
= new wxProgressDialog
716 _T("Progress dialog"),
717 _T("Wait until the thread terminates or press [Cancel]"),
723 wxPD_ESTIMATED_TIME
|
727 // thread is not running yet, no need for crit sect
733 void MyFrame::OnWorkerEvent(wxThreadEvent
& event
)
735 int n
= event
.GetInt();
738 m_dlgProgress
->Destroy();
739 m_dlgProgress
= (wxProgressDialog
*)NULL
;
741 // the dialog is aborted because the event came from another thread, so
742 // we may need to wake up the main event loop for the dialog to be
748 if ( !m_dlgProgress
->Update(n
) )
750 wxCriticalSectionLocker
lock(m_csCancelled
);
757 void MyFrame::OnStartGUIThread(wxCommandEvent
& WXUNUSED(event
))
759 MyImageDialog
dlg(this);
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 BEGIN_EVENT_TABLE(MyImageDialog
, wxDialog
)
770 EVT_THREAD(GUITHREAD_EVENT
, MyImageDialog::OnGUIThreadEvent
)
771 EVT_PAINT(MyImageDialog::OnPaint
)
774 MyImageDialog::MyImageDialog(wxFrame
*parent
)
775 : wxDialog(parent
, wxID_ANY
, "Image created by a secondary thread",
776 wxDefaultPosition
, wxSize(GUITHREAD_BMP_SIZE
,GUITHREAD_BMP_SIZE
)*1.5, wxDEFAULT_DIALOG_STYLE
),
779 m_nCurrentProgress
= 0;
783 // NOTE: no need to lock m_csBmp until the thread isn't started:
786 if (!m_bmp
.Create(GUITHREAD_BMP_SIZE
,GUITHREAD_BMP_SIZE
) || !m_bmp
.IsOk())
788 wxLogError("Couldn't create the bitmap!");
793 wxMemoryDC
dc(m_bmp
);
794 dc
.SetBackground(*wxBLACK_BRUSH
);
797 // draw the bitmap from a secondary thread
798 if ( m_thread
.Create() != wxTHREAD_NO_ERROR
||
799 m_thread
.Run() != wxTHREAD_NO_ERROR
)
801 wxLogError(wxT("Can't create/run thread!"));
806 MyImageDialog::~MyImageDialog()
808 // in case our thread is still running and for some reason we are destroyed,
809 // do wait for the thread to complete as it assumes that its MyImageDialog
810 // pointer is always valid
814 void MyImageDialog::OnGUIThreadEvent(wxThreadEvent
& event
)
816 m_nCurrentProgress
= int(((float)event
.GetInt()*100)/GUITHREAD_NUM_UPDATES
);
821 void MyImageDialog::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
825 const wxSize
& sz
= dc
.GetSize();
829 wxCriticalSectionLocker
locker(m_csBmp
);
830 dc
.DrawBitmap(m_bmp
, (sz
.GetWidth()-GUITHREAD_BMP_SIZE
)/2,
831 (sz
.GetHeight()-GUITHREAD_BMP_SIZE
)/2);
834 // paint a sort of progress bar with a 10px border:
835 dc
.SetBrush(*wxRED_BRUSH
);
836 dc
.DrawRectangle(10,10, m_nCurrentProgress
*(sz
.GetWidth()-20)/100,30);
837 dc
.SetTextForeground(*wxBLUE
);
838 dc
.DrawText(wxString::Format("%d%%", m_nCurrentProgress
),
839 (sz
.GetWidth()-dc
.GetCharWidth()*2)/2,
840 25-dc
.GetCharHeight()/2);
843 // ----------------------------------------------------------------------------
845 // ----------------------------------------------------------------------------
847 MyThread::MyThread(MyFrame
*frame
)
854 MyThread::~MyThread()
856 wxCriticalSectionLocker
locker(wxGetApp().m_critsect
);
858 wxArrayThread
& threads
= wxGetApp().m_threads
;
859 threads
.Remove(this);
861 if ( threads
.IsEmpty() )
863 // signal the main thread that there are no more threads left if it is
865 if ( wxGetApp().m_shuttingDown
)
867 wxGetApp().m_shuttingDown
= false;
869 wxGetApp().m_semAllDone
.Post();
874 wxThread::ExitCode
MyThread::Entry()
878 text
.Printf(wxT("Thread %p started (priority = %u).\n"),
879 GetId(), GetPriority());
881 // wxLogMessage(text); -- test wxLog thread safeness
883 for ( m_count
= 0; m_count
< 10; m_count
++ )
885 // check if the application is shutting down: in this case all threads
886 // should stop a.s.a.p.
888 wxCriticalSectionLocker
locker(wxGetApp().m_critsect
);
889 if ( wxGetApp().m_shuttingDown
)
893 // check if just this thread was asked to exit
897 text
.Printf(wxT("[%u] Thread %p here.\n"), m_count
, GetId());
900 // wxSleep() can't be called from non-GUI thread!
901 wxThread::Sleep(1000);
904 text
.Printf(wxT("Thread %p finished.\n"), GetId());
906 // wxLogMessage(text); -- test wxLog thread safeness
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 // define this symbol to 1 to test if the YieldFor() call in the wxProgressDialog::Update
917 // function provokes a race condition in which the second wxThreadEvent posted by
918 // MyWorkerThread::Entry is processed by the YieldFor() call of wxProgressDialog::Update
919 // and results in the destruction of the progress dialog itself, resulting in a crash later.
920 #define TEST_YIELD_RACE_CONDITION 0
922 MyWorkerThread::MyWorkerThread(MyFrame
*frame
)
929 void MyWorkerThread::OnExit()
933 wxThread::ExitCode
MyWorkerThread::Entry()
935 #if TEST_YIELD_RACE_CONDITION
939 wxThreadEvent
event( wxEVT_COMMAND_THREAD
, WORKER_EVENT
);
942 wxQueueEvent( m_frame
, event
.Clone() );
945 wxQueueEvent( m_frame
, event
.Clone() );
947 for ( m_count
= 0; !m_frame
->Cancelled() && (m_count
< 100); m_count
++ )
949 // check if we were asked to exit
953 // create any type of command event here
954 wxThreadEvent
event( wxEVT_COMMAND_THREAD
, WORKER_EVENT
);
955 event
.SetInt( m_count
);
957 // send in a thread-safe way
958 wxQueueEvent( m_frame
, event
.Clone() );
963 wxThreadEvent
event( wxEVT_COMMAND_THREAD
, WORKER_EVENT
);
964 event
.SetInt(-1); // that's all
965 wxQueueEvent( m_frame
, event
.Clone() );
972 // ----------------------------------------------------------------------------
974 // ----------------------------------------------------------------------------
976 wxThread::ExitCode
MyGUIThread::Entry()
978 for (int i
=0; i
<GUITHREAD_NUM_UPDATES
&& !TestDestroy(); i
++)
980 // inform the GUI toolkit that we're going to use GUI functions
981 // from a secondary thread:
985 wxCriticalSectionLocker
lock(m_dlg
->m_csBmp
);
987 // draw some more stuff on the bitmap
988 wxMemoryDC
dc(m_dlg
->m_bmp
);
989 dc
.SetBrush((i%2
)==0 ? *wxBLUE_BRUSH
: *wxGREEN_BRUSH
);
990 dc
.DrawRectangle(rand()%GUITHREAD_BMP_SIZE
, rand()%GUITHREAD_BMP_SIZE
, 30, 30);
992 // simulate long drawing time:
996 // if we don't release the GUI mutex the MyImageDialog won't be able to refresh
999 // notify the dialog that another piece of our masterpiece is complete:
1000 wxThreadEvent
event( wxEVT_COMMAND_THREAD
, GUITHREAD_EVENT
);
1002 wxQueueEvent( m_dlg
, event
.Clone() );
1004 // give the main thread the time to refresh before we lock the GUI mutex again
1005 // FIXME: find a better way to do this!