X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/38d6b9572cbe98fffe90340396b0e4c43a7fffef..81f5836b8953c11551767e7bf16467824432c0a3:/samples/thread/thread.cpp diff --git a/samples/thread/thread.cpp b/samples/thread/thread.cpp index e4e71d0928..248b3dee44 100644 --- a/samples/thread/thread.cpp +++ b/samples/thread/thread.cpp @@ -1,11 +1,11 @@ ///////////////////////////////////////////////////////////////////////////// // Name: thread.cpp -// Purpose: wxWindows thread sample +// Purpose: wxWidgets thread sample // Author: Guilhem Lavaux, Vadim Zeitlin // Modified by: // Created: 06/16/98 // RCS-ID: $Id$ -// Copyright: (c) 1998-2002 wxWindows team +// Copyright: (c) 1998-2002 wxWidgets team // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// @@ -26,9 +26,12 @@ #include "wx/thread.h" #include "wx/dynarray.h" +#include "wx/numdlg.h" #include "wx/progdlg.h" +#include "../sample.xpm" + // define this to use wxExecute in the exec tests, otherwise just use system #define USE_EXECUTE @@ -46,7 +49,7 @@ class MyApp : public wxApp { public: MyApp(); - virtual ~MyApp(); + virtual ~MyApp(){}; virtual bool OnInit(); @@ -131,7 +134,8 @@ private: // ID for the menu commands enum { - THREAD_QUIT = 1, + THREAD_QUIT = wxID_EXIT, + THREAD_ABOUT = wxID_ABOUT, THREAD_TEXT = 101, THREAD_CLEAR, THREAD_START_THREAD = 201, @@ -145,7 +149,6 @@ enum THREAD_EXEC_THREAD, THREAD_SHOWCPUS, - THREAD_ABOUT, WORKER_EVENT // this one gets sent from the worker thread }; @@ -209,7 +212,7 @@ void MyThread::OnExit() // waiting for us if ( wxGetApp().m_waitingUntilAllDone ) { - wxGetApp().m_waitingUntilAllDone = FALSE; + wxGetApp().m_waitingUntilAllDone = false; wxGetApp().m_semAllDone.Post(); } @@ -354,11 +357,7 @@ END_EVENT_TABLE() MyApp::MyApp() : m_semAllDone() { - m_waitingUntilAllDone = FALSE; -} - -MyApp::~MyApp() -{ + m_waitingUntilAllDone = false; } // `Main program' equivalent, creating windows and returning main app frame @@ -369,7 +368,7 @@ bool MyApp::OnInit() //wxLog::AddTraceMask("thread"); // Create the main frame window - MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWindows threads sample"), + MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWidgets threads sample"), 50, 50, 450, 340); // Make a menubar @@ -404,27 +403,31 @@ bool MyApp::OnInit() menuBar->Append(menuHelp, _T("&Help")); frame->SetMenuBar(menuBar); - + // Show the frame - frame->Show(TRUE); + frame->Show(true); SetTopWindow(frame); - return TRUE; + return true; } // My frame constructor MyFrame::MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h) - : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) + : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) { + SetIcon(wxIcon(sample_xpm)); + m_nRunning = m_nCount = 0; m_dlgProgress = (wxProgressDialog *)NULL; +#if wxUSE_STATUSBAR CreateStatusBar(2); +#endif // wxUSE_STATUSBAR - m_txtctrl = new wxTextCtrl(this, -1, _T(""), wxPoint(0, 0), wxSize(0, 0), + m_txtctrl = new wxTextCtrl(this, wxID_ANY, _T(""), wxPoint(0, 0), wxSize(0, 0), wxTE_MULTILINE | wxTE_READONLY); } @@ -449,7 +452,7 @@ MyFrame::~MyFrame() if ( count ) { // set the flag for MyThread::OnExit() - wxGetApp().m_waitingUntilAllDone = TRUE; + wxGetApp().m_waitingUntilAllDone = true; // stop all threads while ( ! threads.IsEmpty() ) @@ -524,9 +527,11 @@ void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) ) threads.Add(thr); } +#if wxUSE_STATUSBAR wxString msg; msg.Printf(wxT("%d new threads created."), count); SetStatusText(msg, 1); +#endif // wxUSE_STATUSBAR // ...and then start them for ( n = 0; n < count; n++ ) @@ -544,7 +549,9 @@ void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) wxLogError(wxT("Can't start thread!")); } +#if wxUSE_STATUSBAR SetStatusText(_T("New thread started."), 1); +#endif // wxUSE_STATUSBAR } void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) @@ -569,7 +576,9 @@ void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) thread->Delete(); +#if wxUSE_STATUSBAR SetStatusText(_T("Thread stopped."), 1); +#endif // wxUSE_STATUSBAR } } @@ -590,7 +599,9 @@ void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) ) { wxGetApp().m_threads[n]->Resume(); +#if wxUSE_STATUSBAR SetStatusText(_T("Thread resumed."), 1); +#endif // wxUSE_STATUSBAR } } @@ -611,7 +622,9 @@ void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) ) { wxGetApp().m_threads[n]->Pause(); +#if wxUSE_STATUSBAR SetStatusText(_T("Thread paused."), 1); +#endif // wxUSE_STATUSBAR } } @@ -643,7 +656,7 @@ void MyFrame::OnIdle(wxIdleEvent& event) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) { - Close(TRUE); + Close(true); } void MyFrame::OnExecMain(wxCommandEvent& WXUNUSED(event)) @@ -690,7 +703,7 @@ void MyFrame::OnShowCPUs(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) { wxMessageDialog dialog(this, - _T("wxWindows multithreaded application sample\n") + _T("wxWidgets multithreaded application sample\n") _T("(c) 1998 Julian Smart, Guilhem Lavaux\n") _T("(c) 1999 Vadim Zeitlin\n") _T("(c) 2000 Robert Roebling"), @@ -717,6 +730,7 @@ void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event)) if ( thread->Create() != wxTHREAD_NO_ERROR ) { wxLogError(wxT("Can't create thread!")); + return; } m_dlgProgress = new wxProgressDialog @@ -733,7 +747,7 @@ void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event)) ); // thread is not running yet, no need for crit sect - m_cancelled = FALSE; + m_cancelled = false; thread->Run(); } @@ -762,7 +776,7 @@ void MyFrame::OnWorkerEvent(wxCommandEvent& event) { wxCriticalSectionLocker lock(m_critsectWork); - m_cancelled = TRUE; + m_cancelled = true; } } #endif