]>
Commit | Line | Data |
---|---|---|
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(wxThreadEvent& 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 = wxID_HIGHEST+1 // 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 | #define TEST_YIELD_RACE_CONDITION 0 | |
296 | ||
297 | void *MyWorkerThread::Entry() | |
298 | { | |
299 | #if TEST_YIELD_RACE_CONDITION | |
300 | if ( TestDestroy() ) | |
301 | return NULL; | |
302 | ||
303 | wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT ); | |
304 | ||
305 | event.SetInt( 50 ); | |
306 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); | |
307 | ||
308 | event.SetInt(-1); | |
309 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); | |
310 | #else | |
311 | for ( m_count = 0; !m_frame->Cancelled() && (m_count < 100); m_count++ ) | |
312 | { | |
313 | // check if we were asked to exit | |
314 | if ( TestDestroy() ) | |
315 | break; | |
316 | ||
317 | // create any type of command event here | |
318 | wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT ); | |
319 | event.SetInt( m_count ); | |
320 | ||
321 | // send in a thread-safe way | |
322 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); | |
323 | ||
324 | wxMilliSleep(200); | |
325 | } | |
326 | ||
327 | wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT ); | |
328 | event.SetInt(-1); // that's all | |
329 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); | |
330 | #endif | |
331 | ||
332 | return NULL; | |
333 | } | |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // a thread which simply calls wxExecute | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
339 | class MyExecThread : public wxThread | |
340 | { | |
341 | public: | |
342 | MyExecThread(const wxChar *command) : wxThread(wxTHREAD_JOINABLE), | |
343 | m_command(command) | |
344 | { | |
345 | Create(); | |
346 | } | |
347 | ||
348 | virtual ExitCode Entry() | |
349 | { | |
350 | return wxUIntToPtr(EXEC(m_command)); | |
351 | } | |
352 | ||
353 | private: | |
354 | wxString m_command; | |
355 | }; | |
356 | ||
357 | // ---------------------------------------------------------------------------- | |
358 | // implementation | |
359 | // ---------------------------------------------------------------------------- | |
360 | ||
361 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
362 | EVT_MENU(THREAD_QUIT, MyFrame::OnQuit) | |
363 | EVT_MENU(THREAD_CLEAR, MyFrame::OnClear) | |
364 | EVT_MENU(THREAD_START_THREAD, MyFrame::OnStartThread) | |
365 | EVT_MENU(THREAD_START_THREADS, MyFrame::OnStartThreads) | |
366 | EVT_MENU(THREAD_STOP_THREAD, MyFrame::OnStopThread) | |
367 | EVT_MENU(THREAD_PAUSE_THREAD, MyFrame::OnPauseThread) | |
368 | EVT_MENU(THREAD_RESUME_THREAD, MyFrame::OnResumeThread) | |
369 | ||
370 | EVT_MENU(THREAD_EXEC_MAIN, MyFrame::OnExecMain) | |
371 | EVT_MENU(THREAD_EXEC_THREAD, MyFrame::OnExecThread) | |
372 | ||
373 | EVT_MENU(THREAD_SHOWCPUS, MyFrame::OnShowCPUs) | |
374 | EVT_MENU(THREAD_ABOUT, MyFrame::OnAbout) | |
375 | ||
376 | EVT_UPDATE_UI(THREAD_START_WORKER, MyFrame::OnUpdateWorker) | |
377 | EVT_MENU(THREAD_START_WORKER, MyFrame::OnStartWorker) | |
378 | ||
379 | EVT_THREAD(WORKER_EVENT, MyFrame::OnWorkerEvent) | |
380 | ||
381 | EVT_IDLE(MyFrame::OnIdle) | |
382 | END_EVENT_TABLE() | |
383 | ||
384 | MyApp::MyApp() | |
385 | { | |
386 | m_shuttingDown = false; | |
387 | } | |
388 | ||
389 | // `Main program' equivalent, creating windows and returning main app frame | |
390 | bool MyApp::OnInit() | |
391 | { | |
392 | if ( !wxApp::OnInit() ) | |
393 | return false; | |
394 | ||
395 | // uncomment this to get some debugging messages from the trace code | |
396 | // on the console (or just set WXTRACE env variable to include "thread") | |
397 | //wxLog::AddTraceMask("thread"); | |
398 | ||
399 | // Create the main frame window | |
400 | MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWidgets threads sample"), | |
401 | 50, 50, 450, 340); | |
402 | ||
403 | // Make a menubar | |
404 | wxMenuBar *menuBar = new wxMenuBar; | |
405 | ||
406 | wxMenu *menuFile = new wxMenu; | |
407 | menuFile->Append(THREAD_CLEAR, _T("&Clear log\tCtrl-L")); | |
408 | menuFile->AppendSeparator(); | |
409 | menuFile->Append(THREAD_QUIT, _T("E&xit\tAlt-X")); | |
410 | menuBar->Append(menuFile, _T("&File")); | |
411 | ||
412 | wxMenu *menuThread = new wxMenu; | |
413 | menuThread->Append(THREAD_START_THREAD, _T("&Start a new thread\tCtrl-N")); | |
414 | menuThread->Append(THREAD_START_THREADS, _T("Start &many threads at once")); | |
415 | menuThread->Append(THREAD_STOP_THREAD, _T("S&top a running thread\tCtrl-S")); | |
416 | menuThread->AppendSeparator(); | |
417 | menuThread->Append(THREAD_PAUSE_THREAD, _T("&Pause a running thread\tCtrl-P")); | |
418 | menuThread->Append(THREAD_RESUME_THREAD, _T("&Resume suspended thread\tCtrl-R")); | |
419 | menuThread->AppendSeparator(); | |
420 | menuThread->Append(THREAD_START_WORKER, _T("Start &worker thread\tCtrl-W")); | |
421 | menuBar->Append(menuThread, _T("&Thread")); | |
422 | ||
423 | wxMenu *menuExec = new wxMenu; | |
424 | menuExec->Append(THREAD_EXEC_MAIN, _T("&Launch a program from main thread\tF5")); | |
425 | menuExec->Append(THREAD_EXEC_THREAD, _T("L&aunch a program from a thread\tCtrl-F5")); | |
426 | menuBar->Append(menuExec, _T("&Execute")); | |
427 | ||
428 | wxMenu *menuHelp = new wxMenu; | |
429 | menuHelp->Append(THREAD_SHOWCPUS, _T("&Show CPU count")); | |
430 | menuHelp->AppendSeparator(); | |
431 | menuHelp->Append(THREAD_ABOUT, _T("&About...")); | |
432 | menuBar->Append(menuHelp, _T("&Help")); | |
433 | ||
434 | frame->SetMenuBar(menuBar); | |
435 | ||
436 | // Show the frame | |
437 | frame->Show(true); | |
438 | ||
439 | SetTopWindow(frame); | |
440 | ||
441 | return true; | |
442 | } | |
443 | ||
444 | // My frame constructor | |
445 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, | |
446 | int x, int y, int w, int h) | |
447 | : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) | |
448 | { | |
449 | SetIcon(wxIcon(sample_xpm)); | |
450 | ||
451 | m_nRunning = m_nCount = 0; | |
452 | ||
453 | m_dlgProgress = (wxProgressDialog *)NULL; | |
454 | ||
455 | #if wxUSE_STATUSBAR | |
456 | CreateStatusBar(2); | |
457 | #endif // wxUSE_STATUSBAR | |
458 | ||
459 | m_txtctrl = new wxTextCtrl(this, wxID_ANY, _T(""), wxPoint(0, 0), wxSize(0, 0), | |
460 | wxTE_MULTILINE | wxTE_READONLY); | |
461 | } | |
462 | ||
463 | MyFrame::~MyFrame() | |
464 | { | |
465 | // NB: although the OS will terminate all the threads anyhow when the main | |
466 | // one exits, it's good practice to do it ourselves -- even if it's not | |
467 | // completely trivial in this example | |
468 | ||
469 | // tell all the threads to terminate: note that they can't terminate while | |
470 | // we're deleting them because they will block in their OnExit() -- this is | |
471 | // important as otherwise we might access invalid array elements | |
472 | ||
473 | { | |
474 | wxCriticalSectionLocker locker(wxGetApp().m_critsect); | |
475 | ||
476 | // check if we have any threads running first | |
477 | const wxArrayThread& threads = wxGetApp().m_threads; | |
478 | size_t count = threads.GetCount(); | |
479 | ||
480 | if ( !count ) | |
481 | return; | |
482 | ||
483 | // set the flag indicating that all threads should exit | |
484 | wxGetApp().m_shuttingDown = true; | |
485 | } | |
486 | ||
487 | // now wait for them to really terminate | |
488 | wxGetApp().m_semAllDone.Wait(); | |
489 | } | |
490 | ||
491 | MyThread *MyFrame::CreateThread() | |
492 | { | |
493 | MyThread *thread = new MyThread(this); | |
494 | ||
495 | if ( thread->Create() != wxTHREAD_NO_ERROR ) | |
496 | { | |
497 | wxLogError(wxT("Can't create thread!")); | |
498 | } | |
499 | ||
500 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); | |
501 | wxGetApp().m_threads.Add(thread); | |
502 | ||
503 | return thread; | |
504 | } | |
505 | ||
506 | void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) ) | |
507 | { | |
508 | static long s_num; | |
509 | ||
510 | s_num = wxGetNumberFromUser(_T("How many threads to start: "), _T(""), | |
511 | _T("wxThread sample"), s_num, 1, 10000, this); | |
512 | if ( s_num == -1 ) | |
513 | { | |
514 | s_num = 10; | |
515 | ||
516 | return; | |
517 | } | |
518 | ||
519 | unsigned count = unsigned(s_num), n; | |
520 | ||
521 | wxArrayThread threads; | |
522 | ||
523 | // first create them all... | |
524 | for ( n = 0; n < count; n++ ) | |
525 | { | |
526 | wxThread *thr = CreateThread(); | |
527 | ||
528 | // we want to show the effect of SetPriority(): the first thread will | |
529 | // have the lowest priority, the second - the highest, all the rest | |
530 | // the normal one | |
531 | if ( n == 0 ) | |
532 | thr->SetPriority(WXTHREAD_MIN_PRIORITY); | |
533 | else if ( n == 1 ) | |
534 | thr->SetPriority(WXTHREAD_MAX_PRIORITY); | |
535 | else | |
536 | thr->SetPriority(WXTHREAD_DEFAULT_PRIORITY); | |
537 | ||
538 | threads.Add(thr); | |
539 | } | |
540 | ||
541 | #if wxUSE_STATUSBAR | |
542 | wxString msg; | |
543 | msg.Printf(wxT("%d new threads created."), count); | |
544 | SetStatusText(msg, 1); | |
545 | #endif // wxUSE_STATUSBAR | |
546 | ||
547 | // ...and then start them | |
548 | for ( n = 0; n < count; n++ ) | |
549 | { | |
550 | threads[n]->Run(); | |
551 | } | |
552 | } | |
553 | ||
554 | void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) | |
555 | { | |
556 | MyThread *thread = CreateThread(); | |
557 | ||
558 | if ( thread->Run() != wxTHREAD_NO_ERROR ) | |
559 | { | |
560 | wxLogError(wxT("Can't start thread!")); | |
561 | } | |
562 | ||
563 | #if wxUSE_STATUSBAR | |
564 | SetStatusText(_T("New thread started."), 1); | |
565 | #endif // wxUSE_STATUSBAR | |
566 | } | |
567 | ||
568 | void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) | |
569 | { | |
570 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); | |
571 | ||
572 | // stop the last thread | |
573 | if ( wxGetApp().m_threads.IsEmpty() ) | |
574 | { | |
575 | wxLogError(wxT("No thread to stop!")); | |
576 | } | |
577 | else | |
578 | { | |
579 | wxGetApp().m_threads.Last()->Delete(); | |
580 | ||
581 | #if wxUSE_STATUSBAR | |
582 | SetStatusText(_T("Last thread stopped."), 1); | |
583 | #endif // wxUSE_STATUSBAR | |
584 | } | |
585 | } | |
586 | ||
587 | void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) ) | |
588 | { | |
589 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); | |
590 | ||
591 | // resume first suspended thread | |
592 | size_t n = 0, count = wxGetApp().m_threads.Count(); | |
593 | while ( n < count && !wxGetApp().m_threads[n]->IsPaused() ) | |
594 | n++; | |
595 | ||
596 | if ( n == count ) | |
597 | { | |
598 | wxLogError(wxT("No thread to resume!")); | |
599 | } | |
600 | else | |
601 | { | |
602 | wxGetApp().m_threads[n]->Resume(); | |
603 | ||
604 | #if wxUSE_STATUSBAR | |
605 | SetStatusText(_T("Thread resumed."), 1); | |
606 | #endif // wxUSE_STATUSBAR | |
607 | } | |
608 | } | |
609 | ||
610 | void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) ) | |
611 | { | |
612 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); | |
613 | ||
614 | // pause last running thread | |
615 | int n = wxGetApp().m_threads.Count() - 1; | |
616 | while ( n >= 0 && !wxGetApp().m_threads[n]->IsRunning() ) | |
617 | n--; | |
618 | ||
619 | if ( n < 0 ) | |
620 | { | |
621 | wxLogError(wxT("No thread to pause!")); | |
622 | } | |
623 | else | |
624 | { | |
625 | wxGetApp().m_threads[n]->Pause(); | |
626 | ||
627 | #if wxUSE_STATUSBAR | |
628 | SetStatusText(_T("Thread paused."), 1); | |
629 | #endif // wxUSE_STATUSBAR | |
630 | } | |
631 | } | |
632 | ||
633 | void MyFrame::OnIdle(wxIdleEvent& event) | |
634 | { | |
635 | DoLogThreadMessages(); | |
636 | ||
637 | UpdateThreadStatus(); | |
638 | ||
639 | event.Skip(); | |
640 | } | |
641 | ||
642 | void MyFrame::DoLogThreadMessages() | |
643 | { | |
644 | wxCriticalSectionLocker lock(m_csMessages); | |
645 | ||
646 | const size_t count = m_messages.size(); | |
647 | for ( size_t n = 0; n < count; n++ ) | |
648 | { | |
649 | m_txtctrl->AppendText(m_messages[n]); | |
650 | } | |
651 | ||
652 | m_messages.clear(); | |
653 | } | |
654 | ||
655 | void MyFrame::UpdateThreadStatus() | |
656 | { | |
657 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); | |
658 | ||
659 | // update the counts of running/total threads | |
660 | size_t nRunning = 0, | |
661 | nCount = wxGetApp().m_threads.Count(); | |
662 | for ( size_t n = 0; n < nCount; n++ ) | |
663 | { | |
664 | if ( wxGetApp().m_threads[n]->IsRunning() ) | |
665 | nRunning++; | |
666 | } | |
667 | ||
668 | if ( nCount != m_nCount || nRunning != m_nRunning ) | |
669 | { | |
670 | m_nRunning = nRunning; | |
671 | m_nCount = nCount; | |
672 | ||
673 | wxLogStatus(this, wxT("%u threads total, %u running."), unsigned(nCount), unsigned(nRunning)); | |
674 | } | |
675 | //else: avoid flicker - don't print anything | |
676 | } | |
677 | ||
678 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
679 | { | |
680 | Close(true); | |
681 | } | |
682 | ||
683 | void MyFrame::OnExecMain(wxCommandEvent& WXUNUSED(event)) | |
684 | { | |
685 | wxLogMessage(wxT("The exit code from the main program is %ld"), | |
686 | EXEC(_T("/bin/echo \"main program\""))); | |
687 | } | |
688 | ||
689 | void MyFrame::OnExecThread(wxCommandEvent& WXUNUSED(event)) | |
690 | { | |
691 | MyExecThread thread(wxT("/bin/echo \"child thread\"")); | |
692 | thread.Run(); | |
693 | ||
694 | wxLogMessage(wxT("The exit code from a child thread is %ld"), | |
695 | (long)wxPtrToUInt(thread.Wait())); | |
696 | } | |
697 | ||
698 | void MyFrame::OnShowCPUs(wxCommandEvent& WXUNUSED(event)) | |
699 | { | |
700 | wxString msg; | |
701 | ||
702 | int nCPUs = wxThread::GetCPUCount(); | |
703 | switch ( nCPUs ) | |
704 | { | |
705 | case -1: | |
706 | msg = _T("Unknown number of CPUs"); | |
707 | break; | |
708 | ||
709 | case 0: | |
710 | msg = _T("WARNING: you're running without any CPUs!"); | |
711 | break; | |
712 | ||
713 | case 1: | |
714 | msg = _T("This system only has one CPU."); | |
715 | break; | |
716 | ||
717 | default: | |
718 | msg.Printf(wxT("This system has %d CPUs"), nCPUs); | |
719 | } | |
720 | ||
721 | wxLogMessage(msg); | |
722 | } | |
723 | ||
724 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
725 | { | |
726 | wxMessageDialog dialog(this, | |
727 | _T("wxWidgets multithreaded application sample\n") | |
728 | _T("(c) 1998 Julian Smart, Guilhem Lavaux\n") | |
729 | _T("(c) 1999 Vadim Zeitlin\n") | |
730 | _T("(c) 2000 Robert Roebling"), | |
731 | _T("About wxThread sample"), | |
732 | wxOK | wxICON_INFORMATION); | |
733 | ||
734 | dialog.ShowModal(); | |
735 | } | |
736 | ||
737 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
738 | { | |
739 | m_txtctrl->Clear(); | |
740 | } | |
741 | ||
742 | void MyFrame::OnUpdateWorker(wxUpdateUIEvent& event) | |
743 | { | |
744 | event.Enable( m_dlgProgress == NULL ); | |
745 | } | |
746 | ||
747 | void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event)) | |
748 | { | |
749 | MyWorkerThread *thread = new MyWorkerThread(this); | |
750 | ||
751 | if ( thread->Create() != wxTHREAD_NO_ERROR ) | |
752 | { | |
753 | wxLogError(wxT("Can't create thread!")); | |
754 | return; | |
755 | } | |
756 | ||
757 | m_dlgProgress = new wxProgressDialog | |
758 | ( | |
759 | _T("Progress dialog"), | |
760 | _T("Wait until the thread terminates or press [Cancel]"), | |
761 | 100, | |
762 | this, | |
763 | wxPD_CAN_ABORT | | |
764 | wxPD_APP_MODAL | | |
765 | wxPD_ELAPSED_TIME | | |
766 | wxPD_ESTIMATED_TIME | | |
767 | wxPD_REMAINING_TIME | |
768 | ); | |
769 | ||
770 | // thread is not running yet, no need for crit sect | |
771 | m_cancelled = false; | |
772 | ||
773 | thread->Run(); | |
774 | } | |
775 | ||
776 | void MyFrame::OnWorkerEvent(wxThreadEvent& event) | |
777 | { | |
778 | int n = event.GetInt(); | |
779 | if ( n == -1 ) | |
780 | { | |
781 | m_dlgProgress->Destroy(); | |
782 | m_dlgProgress = (wxProgressDialog *)NULL; | |
783 | ||
784 | // the dialog is aborted because the event came from another thread, so | |
785 | // we may need to wake up the main event loop for the dialog to be | |
786 | // really closed | |
787 | wxWakeUpIdle(); | |
788 | } | |
789 | else | |
790 | { | |
791 | if ( !m_dlgProgress->Update(n) ) | |
792 | { | |
793 | wxCriticalSectionLocker lock(m_critsectWork); | |
794 | ||
795 | m_cancelled = true; | |
796 | } | |
797 | } | |
798 | } | |
799 | ||
800 | bool MyFrame::Cancelled() | |
801 | { | |
802 | wxCriticalSectionLocker lock(m_critsectWork); | |
803 | ||
804 | return m_cancelled; | |
805 | } |