1998-06-16 17:17:02 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2000-03-14 19:35:40 +00:00
|
|
|
// Name: thread.cpp
|
1998-06-16 17:17:02 +00:00
|
|
|
// Purpose: wxWindows thread sample
|
2002-07-01 14:55:39 +00:00
|
|
|
// Author: Guilhem Lavaux, Vadim Zeitlin
|
1998-06-16 17:17:02 +00:00
|
|
|
// Modified by:
|
|
|
|
// Created: 06/16/98
|
|
|
|
// RCS-ID: $Id$
|
2002-07-01 14:55:39 +00:00
|
|
|
// Copyright: (c) 1998-2002 wxWindows team
|
1999-01-02 22:55:03 +00:00
|
|
|
// Licence: wxWindows license
|
1998-06-16 17:17:02 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
1999-01-02 22:55:03 +00:00
|
|
|
#pragma hdrstop
|
1998-06-16 17:17:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
1999-01-02 22:55:03 +00:00
|
|
|
#include "wx/wx.h"
|
1998-06-16 17:17:02 +00:00
|
|
|
#endif
|
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
#if !wxUSE_THREADS
|
|
|
|
#error "This sample requires thread support!"
|
|
|
|
#endif // wxUSE_THREADS
|
|
|
|
|
1998-06-16 17:17:02 +00:00
|
|
|
#include "wx/thread.h"
|
|
|
|
#include "wx/dynarray.h"
|
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
#include "wx/progdlg.h"
|
|
|
|
|
2002-07-01 14:55:39 +00:00
|
|
|
// define this to use wxExecute in the exec tests, otherwise just use system
|
2002-04-12 19:05:11 +00:00
|
|
|
#define USE_EXECUTE
|
2002-07-01 14:55:39 +00:00
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
#ifdef USE_EXECUTE
|
|
|
|
#define EXEC(cmd) wxExecute((cmd), wxEXEC_SYNC)
|
|
|
|
#else
|
|
|
|
#define EXEC(cmd) system(cmd)
|
|
|
|
#endif
|
2000-02-23 19:04:31 +00:00
|
|
|
|
1999-08-26 18:04:46 +00:00
|
|
|
class MyThread;
|
|
|
|
WX_DEFINE_ARRAY(wxThread *, wxArrayThread);
|
|
|
|
|
1998-06-16 17:17:02 +00:00
|
|
|
// Define a new application type
|
1999-01-17 22:39:58 +00:00
|
|
|
class MyApp : public wxApp
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
1999-10-19 13:58:16 +00:00
|
|
|
public:
|
2002-07-01 14:55:39 +00:00
|
|
|
MyApp();
|
|
|
|
virtual ~MyApp();
|
|
|
|
|
1999-10-19 13:58:16 +00:00
|
|
|
virtual bool OnInit();
|
|
|
|
|
|
|
|
public:
|
|
|
|
// all the threads currently alive - as soon as the thread terminates, it's
|
|
|
|
// removed from the array
|
|
|
|
wxArrayThread m_threads;
|
|
|
|
|
|
|
|
// crit section protects access to all of the arrays below
|
|
|
|
wxCriticalSection m_critsect;
|
2002-07-01 14:55:39 +00:00
|
|
|
|
|
|
|
// the (mutex, condition) pair used to wait for the threads to exit, see
|
|
|
|
// MyFrame::OnQuit()
|
|
|
|
wxMutex m_mutexAllDone;
|
|
|
|
wxCondition m_condAllDone;
|
|
|
|
|
|
|
|
// the last exiting thread should signal m_condAllDone if this is true
|
|
|
|
// (protected by the same m_critsect)
|
|
|
|
bool m_waitingUntilAllDone;
|
1998-06-16 17:17:02 +00:00
|
|
|
};
|
|
|
|
|
1999-08-26 18:04:46 +00:00
|
|
|
// Create a new application object
|
1999-10-19 13:58:16 +00:00
|
|
|
IMPLEMENT_APP(MyApp)
|
1998-06-16 17:17:02 +00:00
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
1998-12-27 00:54:53 +00:00
|
|
|
public:
|
|
|
|
// ctor
|
1999-01-17 22:39:58 +00:00
|
|
|
MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// operations
|
|
|
|
void WriteText(const wxString& text) { m_txtctrl->WriteText(text); }
|
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
// accessors for MyWorkerThread (called in its context!)
|
|
|
|
bool Cancelled();
|
|
|
|
|
2002-07-01 14:55:39 +00:00
|
|
|
protected:
|
1998-12-27 00:54:53 +00:00
|
|
|
// callbacks
|
1998-06-16 17:17:02 +00:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
1999-01-09 00:28:27 +00:00
|
|
|
void OnClear(wxCommandEvent& event);
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1998-06-16 17:17:02 +00:00
|
|
|
void OnStartThread(wxCommandEvent& event);
|
1999-01-22 16:21:24 +00:00
|
|
|
void OnStartThreads(wxCommandEvent& event);
|
1998-06-16 17:17:02 +00:00
|
|
|
void OnStopThread(wxCommandEvent& event);
|
|
|
|
void OnPauseThread(wxCommandEvent& event);
|
1998-12-27 00:54:53 +00:00
|
|
|
void OnResumeThread(wxCommandEvent& event);
|
2000-01-29 02:34:46 +00:00
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
void OnStartWorker(wxCommandEvent& event);
|
|
|
|
void OnWorkerEvent(wxCommandEvent& event);
|
2000-01-29 02:34:46 +00:00
|
|
|
void OnUpdateWorker(wxUpdateUIEvent& event);
|
1998-12-27 00:54:53 +00:00
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
void OnExecMain(wxCommandEvent& event);
|
|
|
|
void OnExecThread(wxCommandEvent& event);
|
|
|
|
|
|
|
|
void OnShowCPUs(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
void OnIdle(wxIdleEvent &event);
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
private:
|
1999-01-22 16:21:24 +00:00
|
|
|
// helper function - creates a new thread (but doesn't run it)
|
|
|
|
MyThread *CreateThread();
|
1999-06-01 15:32:12 +00:00
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// just some place to put our messages in
|
|
|
|
wxTextCtrl *m_txtctrl;
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1999-01-22 16:21:24 +00:00
|
|
|
// remember the number of running threads and total number of threads
|
|
|
|
size_t m_nRunning, m_nCount;
|
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
// the progress dialog which we show while worker thread is running
|
|
|
|
wxProgressDialog *m_dlgProgress;
|
|
|
|
|
|
|
|
// was the worker thread cancelled by user?
|
|
|
|
bool m_cancelled;
|
|
|
|
|
|
|
|
// protects m_cancelled
|
|
|
|
wxCriticalSection m_critsectWork;
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
DECLARE_EVENT_TABLE()
|
1998-06-16 17:17:02 +00:00
|
|
|
};
|
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
// ID for the menu commands
|
|
|
|
enum
|
|
|
|
{
|
2002-03-27 17:15:13 +00:00
|
|
|
THREAD_QUIT = 1,
|
|
|
|
THREAD_TEXT = 101,
|
|
|
|
THREAD_CLEAR,
|
|
|
|
THREAD_START_THREAD = 201,
|
|
|
|
THREAD_START_THREADS,
|
|
|
|
THREAD_STOP_THREAD,
|
|
|
|
THREAD_PAUSE_THREAD,
|
|
|
|
THREAD_RESUME_THREAD,
|
|
|
|
THREAD_START_WORKER,
|
|
|
|
|
|
|
|
THREAD_EXEC_MAIN,
|
|
|
|
THREAD_EXEC_THREAD,
|
|
|
|
|
|
|
|
THREAD_SHOWCPUS,
|
|
|
|
THREAD_ABOUT,
|
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
WORKER_EVENT // this one gets sent from the worker thread
|
|
|
|
};
|
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 12:02:57 +00:00
|
|
|
// GUI thread
|
2002-03-27 17:15:13 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 12:02:57 +00:00
|
|
|
|
1999-01-09 00:28:27 +00:00
|
|
|
class MyThread : public wxThread
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
1998-12-27 00:54:53 +00:00
|
|
|
public:
|
1998-06-16 17:17:02 +00:00
|
|
|
MyThread(MyFrame *frame);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
|
|
|
// thread execution starts here
|
|
|
|
virtual void *Entry();
|
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// called when the thread exits - whether it terminates normally or is
|
|
|
|
// stopped with Delete() (but not when it is Kill()ed!)
|
1999-01-09 00:28:27 +00:00
|
|
|
virtual void OnExit();
|
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
// write something to the text control
|
|
|
|
void WriteText(const wxString& text);
|
1998-12-27 00:54:53 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
size_t m_count;
|
1998-06-16 17:17:02 +00:00
|
|
|
MyFrame *m_frame;
|
|
|
|
};
|
|
|
|
|
|
|
|
MyThread::MyThread(MyFrame *frame)
|
1998-12-27 00:54:53 +00:00
|
|
|
: wxThread()
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
1998-12-27 00:54:53 +00:00
|
|
|
m_count = 0;
|
|
|
|
m_frame = frame;
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
void MyThread::WriteText(const wxString& text)
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
// before doing any GUI calls we must ensure that this thread is the only
|
|
|
|
// one doing it!
|
1999-06-01 15:32:12 +00:00
|
|
|
|
1999-05-22 08:01:17 +00:00
|
|
|
wxMutexGuiEnter();
|
1999-06-01 15:32:12 +00:00
|
|
|
|
1999-11-22 19:44:25 +00:00
|
|
|
msg << text;
|
1999-01-02 22:55:03 +00:00
|
|
|
m_frame->WriteText(msg);
|
2000-01-29 02:34:46 +00:00
|
|
|
|
1999-05-22 08:01:17 +00:00
|
|
|
wxMutexGuiLeave();
|
1999-01-09 00:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyThread::OnExit()
|
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
wxCriticalSectionLocker locker(wxGetApp().m_critsect);
|
|
|
|
|
2002-07-01 14:55:39 +00:00
|
|
|
wxArrayThread& threads = wxGetApp().m_threads;
|
|
|
|
threads.Remove(this);
|
|
|
|
|
|
|
|
if ( threads.IsEmpty() )
|
|
|
|
{
|
|
|
|
// signal the main thread that there are no more threads left if it is
|
|
|
|
// waiting for us
|
|
|
|
if ( wxGetApp().m_waitingUntilAllDone )
|
|
|
|
{
|
|
|
|
wxGetApp().m_waitingUntilAllDone = FALSE;
|
|
|
|
|
|
|
|
wxMutexLocker lock(wxGetApp().m_mutexAllDone);
|
|
|
|
wxGetApp().m_condAllDone.Signal();
|
|
|
|
}
|
|
|
|
}
|
1999-01-02 22:55:03 +00:00
|
|
|
}
|
|
|
|
|
1998-06-16 17:17:02 +00:00
|
|
|
void *MyThread::Entry()
|
|
|
|
{
|
1998-12-27 00:54:53 +00:00
|
|
|
wxString text;
|
1999-01-02 22:55:03 +00:00
|
|
|
|
Deprecated wxSizer::Remove( wxWindow* ), s/Remove/Detach/ in most places.
Made wxSizer child list typesafe. I've not added the wxList implicit
conversion kludge yet, let's see who complains first perhaps..
Deprecated wxSizer::{G,S}etOption in favour of {G,S}etProportion in line
with the parameter name change in the docs.
Added {G,S}etSpacer consistent with the accessors for windows/sizers.
Made all wxSizer index parameters size_t -- we support no sensible
interpretation for negative indexes in them. Hopefully this will
cause no real problems, but code doing (eg. Remove( 0 )) will need
to change to use 0u to resolve the ambiguity with overloaded members.
This is probably a Good Thing though, ymmv.
s/FALSE/false/g ; s/TRUE/true/g ; s/wxASSERT/wxASSERT_MSG/g in sizer.{cpp,h}
Fixed (I hope) the brokenness in wxSizer::Show -- I have no code to test
this yet, so it's a blind change, but spacers should now be hidden correctly
instead of ignored, and it should be properly reversable over multiple
calls now too.
removed pointless private scoping around DECLARE_CLASS macros.
Replace 0's I added previously with NULL -- not like that will end the
email thread either..
Added Add( wxSizerItem * ) & co. There are probably a couple of other
places we can usefully do something like this too. Stopped short of
refactoring everything to raise some issues about sizer method recursion
on -dev.
Updated wxSizer docs some more, they are still incomplete but getting
better.
wrapped KeyCode in wxDEPRECATED, converted all (gtk build) instances
to GetKeyCode. There may be a few left for other ports.
Fixed a couple of other random compile warnings along the way.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18616 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-01-07 10:22:07 +00:00
|
|
|
text.Printf(wxT("Thread 0x%lx started (priority = %u).\n"),
|
1999-11-27 22:57:06 +00:00
|
|
|
GetId(), GetPriority());
|
1999-01-02 22:55:03 +00:00
|
|
|
WriteText(text);
|
2000-02-04 20:27:10 +00:00
|
|
|
// wxLogMessage(text); -- test wxLog thread safeness
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1999-01-09 00:28:27 +00:00
|
|
|
for ( m_count = 0; m_count < 10; m_count++ )
|
1999-01-02 22:55:03 +00:00
|
|
|
{
|
|
|
|
// check if we were asked to exit
|
|
|
|
if ( TestDestroy() )
|
|
|
|
break;
|
|
|
|
|
Deprecated wxSizer::Remove( wxWindow* ), s/Remove/Detach/ in most places.
Made wxSizer child list typesafe. I've not added the wxList implicit
conversion kludge yet, let's see who complains first perhaps..
Deprecated wxSizer::{G,S}etOption in favour of {G,S}etProportion in line
with the parameter name change in the docs.
Added {G,S}etSpacer consistent with the accessors for windows/sizers.
Made all wxSizer index parameters size_t -- we support no sensible
interpretation for negative indexes in them. Hopefully this will
cause no real problems, but code doing (eg. Remove( 0 )) will need
to change to use 0u to resolve the ambiguity with overloaded members.
This is probably a Good Thing though, ymmv.
s/FALSE/false/g ; s/TRUE/true/g ; s/wxASSERT/wxASSERT_MSG/g in sizer.{cpp,h}
Fixed (I hope) the brokenness in wxSizer::Show -- I have no code to test
this yet, so it's a blind change, but spacers should now be hidden correctly
instead of ignored, and it should be properly reversable over multiple
calls now too.
removed pointless private scoping around DECLARE_CLASS macros.
Replace 0's I added previously with NULL -- not like that will end the
email thread either..
Added Add( wxSizerItem * ) & co. There are probably a couple of other
places we can usefully do something like this too. Stopped short of
refactoring everything to raise some issues about sizer method recursion
on -dev.
Updated wxSizer docs some more, they are still incomplete but getting
better.
wrapped KeyCode in wxDEPRECATED, converted all (gtk build) instances
to GetKeyCode. There may be a few left for other ports.
Fixed a couple of other random compile warnings along the way.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18616 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-01-07 10:22:07 +00:00
|
|
|
text.Printf(wxT("[%u] Thread 0x%lx here.\n"), m_count, GetId());
|
1999-01-02 22:55:03 +00:00
|
|
|
WriteText(text);
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// wxSleep() can't be called from non-GUI thread!
|
|
|
|
wxThread::Sleep(1000);
|
1998-12-27 00:54:53 +00:00
|
|
|
}
|
1999-01-02 22:55:03 +00:00
|
|
|
|
Deprecated wxSizer::Remove( wxWindow* ), s/Remove/Detach/ in most places.
Made wxSizer child list typesafe. I've not added the wxList implicit
conversion kludge yet, let's see who complains first perhaps..
Deprecated wxSizer::{G,S}etOption in favour of {G,S}etProportion in line
with the parameter name change in the docs.
Added {G,S}etSpacer consistent with the accessors for windows/sizers.
Made all wxSizer index parameters size_t -- we support no sensible
interpretation for negative indexes in them. Hopefully this will
cause no real problems, but code doing (eg. Remove( 0 )) will need
to change to use 0u to resolve the ambiguity with overloaded members.
This is probably a Good Thing though, ymmv.
s/FALSE/false/g ; s/TRUE/true/g ; s/wxASSERT/wxASSERT_MSG/g in sizer.{cpp,h}
Fixed (I hope) the brokenness in wxSizer::Show -- I have no code to test
this yet, so it's a blind change, but spacers should now be hidden correctly
instead of ignored, and it should be properly reversable over multiple
calls now too.
removed pointless private scoping around DECLARE_CLASS macros.
Replace 0's I added previously with NULL -- not like that will end the
email thread either..
Added Add( wxSizerItem * ) & co. There are probably a couple of other
places we can usefully do something like this too. Stopped short of
refactoring everything to raise some issues about sizer method recursion
on -dev.
Updated wxSizer docs some more, they are still incomplete but getting
better.
wrapped KeyCode in wxDEPRECATED, converted all (gtk build) instances
to GetKeyCode. There may be a few left for other ports.
Fixed a couple of other random compile warnings along the way.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18616 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-01-07 10:22:07 +00:00
|
|
|
text.Printf(wxT("Thread 0x%lx finished.\n"), GetId());
|
1999-01-02 22:55:03 +00:00
|
|
|
WriteText(text);
|
2000-02-04 20:27:10 +00:00
|
|
|
// wxLogMessage(text); -- test wxLog thread safeness
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
return NULL;
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 12:02:57 +00:00
|
|
|
// worker thread
|
2002-03-27 17:15:13 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 12:02:57 +00:00
|
|
|
|
|
|
|
class MyWorkerThread : public wxThread
|
1999-01-02 22:55:03 +00:00
|
|
|
{
|
1999-12-30 12:02:57 +00:00
|
|
|
public:
|
|
|
|
MyWorkerThread(MyFrame *frame);
|
|
|
|
|
|
|
|
// thread execution starts here
|
|
|
|
virtual void *Entry();
|
|
|
|
|
|
|
|
// called when the thread exits - whether it terminates normally or is
|
|
|
|
// stopped with Delete() (but not when it is Kill()ed!)
|
|
|
|
virtual void OnExit();
|
|
|
|
|
|
|
|
public:
|
|
|
|
MyFrame *m_frame;
|
|
|
|
size_t m_count;
|
1999-01-02 22:55:03 +00:00
|
|
|
};
|
1998-06-16 17:17:02 +00:00
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
MyWorkerThread::MyWorkerThread(MyFrame *frame)
|
|
|
|
: wxThread()
|
|
|
|
{
|
|
|
|
m_frame = frame;
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWorkerThread::OnExit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MyWorkerThread::Entry()
|
|
|
|
{
|
2000-01-29 02:34:46 +00:00
|
|
|
for ( m_count = 0; !m_frame->Cancelled() && (m_count < 100); m_count++ )
|
1999-12-30 12:02:57 +00:00
|
|
|
{
|
|
|
|
// check if we were asked to exit
|
|
|
|
if ( TestDestroy() )
|
|
|
|
break;
|
2000-01-29 02:34:46 +00:00
|
|
|
|
|
|
|
// create any type of command event here
|
1999-12-30 12:02:57 +00:00
|
|
|
wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
|
2000-01-29 02:34:46 +00:00
|
|
|
event.SetInt( m_count );
|
|
|
|
|
2000-01-02 16:25:28 +00:00
|
|
|
// send in a thread-safe way
|
1999-12-30 12:02:57 +00:00
|
|
|
wxPostEvent( m_frame, event );
|
2000-01-29 02:34:46 +00:00
|
|
|
|
2000-01-02 16:25:28 +00:00
|
|
|
// wxSleep() can't be called from non-main thread!
|
2000-01-29 02:34:46 +00:00
|
|
|
wxThread::Sleep(200);
|
1999-12-30 12:02:57 +00:00
|
|
|
}
|
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
|
|
|
|
event.SetInt(-1); // that's all
|
|
|
|
wxPostEvent( m_frame, event );
|
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// a thread which simply calls wxExecute
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyExecThread : public wxThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyExecThread(const wxChar *command) : wxThread(wxTHREAD_JOINABLE),
|
|
|
|
m_command(command)
|
|
|
|
{
|
|
|
|
Create();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ExitCode Entry()
|
|
|
|
{
|
|
|
|
return (ExitCode)EXEC(m_command);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxString m_command;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 12:02:57 +00:00
|
|
|
|
1998-06-16 17:17:02 +00:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2002-03-27 17:15:13 +00:00
|
|
|
EVT_MENU(THREAD_QUIT, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(THREAD_CLEAR, MyFrame::OnClear)
|
|
|
|
EVT_MENU(THREAD_START_THREAD, MyFrame::OnStartThread)
|
|
|
|
EVT_MENU(THREAD_START_THREADS, MyFrame::OnStartThreads)
|
|
|
|
EVT_MENU(THREAD_STOP_THREAD, MyFrame::OnStopThread)
|
|
|
|
EVT_MENU(THREAD_PAUSE_THREAD, MyFrame::OnPauseThread)
|
|
|
|
EVT_MENU(THREAD_RESUME_THREAD, MyFrame::OnResumeThread)
|
|
|
|
|
|
|
|
EVT_MENU(THREAD_EXEC_MAIN, MyFrame::OnExecMain)
|
|
|
|
EVT_MENU(THREAD_EXEC_THREAD, MyFrame::OnExecThread)
|
|
|
|
|
|
|
|
EVT_MENU(THREAD_SHOWCPUS, MyFrame::OnShowCPUs)
|
|
|
|
EVT_MENU(THREAD_ABOUT, MyFrame::OnAbout)
|
|
|
|
|
|
|
|
EVT_UPDATE_UI(THREAD_START_WORKER, MyFrame::OnUpdateWorker)
|
|
|
|
EVT_MENU(THREAD_START_WORKER, MyFrame::OnStartWorker)
|
1999-12-30 12:02:57 +00:00
|
|
|
EVT_MENU(WORKER_EVENT, MyFrame::OnWorkerEvent)
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
EVT_IDLE(MyFrame::OnIdle)
|
1998-06-16 17:17:02 +00:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2002-07-01 14:55:39 +00:00
|
|
|
MyApp::MyApp()
|
|
|
|
: m_condAllDone(m_mutexAllDone)
|
|
|
|
{
|
|
|
|
// the mutex associated with a condition must be initially locked, it will
|
|
|
|
// only be unlocked when we call Wait()
|
|
|
|
m_mutexAllDone.Lock();
|
|
|
|
|
|
|
|
m_waitingUntilAllDone = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyApp::~MyApp()
|
|
|
|
{
|
|
|
|
// the mutex must be unlocked before being destroyed
|
|
|
|
m_mutexAllDone.Unlock();
|
|
|
|
}
|
|
|
|
|
1998-06-16 17:17:02 +00:00
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
1999-01-02 22:55:03 +00:00
|
|
|
bool MyApp::OnInit()
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
2002-03-27 17:15:13 +00:00
|
|
|
// uncomment this to get some debugging messages from the trace code
|
|
|
|
// on the console (or just set WXTRACE env variable to include "thread")
|
|
|
|
//wxLog::AddTraceMask("thread");
|
2000-02-23 19:04:31 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// Create the main frame window
|
2002-12-14 18:13:27 +00:00
|
|
|
MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWindows threads sample"),
|
1999-01-09 00:28:27 +00:00
|
|
|
50, 50, 450, 340);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// Make a menubar
|
2002-03-27 17:15:13 +00:00
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
|
|
|
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
2002-12-14 18:13:27 +00:00
|
|
|
menuFile->Append(THREAD_CLEAR, _T("&Clear log\tCtrl-L"));
|
2002-03-27 17:15:13 +00:00
|
|
|
menuFile->AppendSeparator();
|
2002-12-14 18:13:27 +00:00
|
|
|
menuFile->Append(THREAD_QUIT, _T("E&xit\tAlt-X"));
|
|
|
|
menuBar->Append(menuFile, _T("&File"));
|
2002-03-27 17:15:13 +00:00
|
|
|
|
|
|
|
wxMenu *menuThread = new wxMenu;
|
2002-12-14 18:13:27 +00:00
|
|
|
menuThread->Append(THREAD_START_THREAD, _T("&Start a new thread\tCtrl-N"));
|
|
|
|
menuThread->Append(THREAD_START_THREADS, _T("Start &many threads at once"));
|
|
|
|
menuThread->Append(THREAD_STOP_THREAD, _T("S&top a running thread\tCtrl-S"));
|
2002-03-27 17:15:13 +00:00
|
|
|
menuThread->AppendSeparator();
|
2002-12-14 18:13:27 +00:00
|
|
|
menuThread->Append(THREAD_PAUSE_THREAD, _T("&Pause a running thread\tCtrl-P"));
|
|
|
|
menuThread->Append(THREAD_RESUME_THREAD, _T("&Resume suspended thread\tCtrl-R"));
|
2002-03-27 17:15:13 +00:00
|
|
|
menuThread->AppendSeparator();
|
2002-12-14 18:13:27 +00:00
|
|
|
menuThread->Append(THREAD_START_WORKER, _T("Start &worker thread\tCtrl-W"));
|
|
|
|
menuBar->Append(menuThread, _T("&Thread"));
|
2002-03-27 17:15:13 +00:00
|
|
|
|
|
|
|
wxMenu *menuExec = new wxMenu;
|
2002-12-14 18:13:27 +00:00
|
|
|
menuExec->Append(THREAD_EXEC_MAIN, _T("&Launch a program from main thread\tF5"));
|
|
|
|
menuExec->Append(THREAD_EXEC_THREAD, _T("L&aunch a program from a thread\tCtrl-F5"));
|
|
|
|
menuBar->Append(menuExec, _T("&Execute"));
|
2002-03-27 17:15:13 +00:00
|
|
|
|
|
|
|
wxMenu *menuHelp = new wxMenu;
|
2002-12-14 18:13:27 +00:00
|
|
|
menuHelp->Append(THREAD_SHOWCPUS, _T("&Show CPU count"));
|
2002-03-27 17:15:13 +00:00
|
|
|
menuHelp->AppendSeparator();
|
2002-12-14 18:13:27 +00:00
|
|
|
menuHelp->Append(THREAD_ABOUT, _T("&About..."));
|
|
|
|
menuBar->Append(menuHelp, _T("&Help"));
|
2002-03-27 17:15:13 +00:00
|
|
|
|
|
|
|
frame->SetMenuBar(menuBar);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// Show the frame
|
|
|
|
frame->Show(TRUE);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
SetTopWindow(frame);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
return TRUE;
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// My frame constructor
|
1999-01-17 22:39:58 +00:00
|
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title,
|
|
|
|
int x, int y, int w, int h)
|
1998-12-27 00:54:53 +00:00
|
|
|
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
1999-01-22 16:21:24 +00:00
|
|
|
m_nRunning = m_nCount = 0;
|
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
m_dlgProgress = (wxProgressDialog *)NULL;
|
|
|
|
|
1999-01-22 16:21:24 +00:00
|
|
|
CreateStatusBar(2);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
2002-12-14 18:13:27 +00:00
|
|
|
m_txtctrl = new wxTextCtrl(this, -1, _T(""), wxPoint(0, 0), wxSize(0, 0),
|
1999-01-09 00:28:27 +00:00
|
|
|
wxTE_MULTILINE | wxTE_READONLY);
|
1998-06-16 17:17:02 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
}
|
1998-06-16 17:17:02 +00:00
|
|
|
|
1999-01-22 16:21:24 +00:00
|
|
|
MyThread *MyFrame::CreateThread()
|
1998-12-27 00:54:53 +00:00
|
|
|
{
|
|
|
|
MyThread *thread = new MyThread(this);
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
if ( thread->Create() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("Can't create thread!"));
|
1999-01-17 22:39:58 +00:00
|
|
|
}
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1999-08-26 18:04:46 +00:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
|
|
|
wxGetApp().m_threads.Add(thread);
|
1999-01-17 22:39:58 +00:00
|
|
|
|
1999-01-22 16:21:24 +00:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
1999-11-27 22:57:06 +00:00
|
|
|
static long s_num = 10;
|
|
|
|
|
2002-12-14 18:13:27 +00:00
|
|
|
s_num = wxGetNumberFromUser(_T("How many threads to start: "), _T(""),
|
|
|
|
_T("wxThread sample"), s_num, 1, 10000, this);
|
1999-11-27 22:57:06 +00:00
|
|
|
if ( s_num == -1 )
|
|
|
|
{
|
|
|
|
s_num = 10;
|
1999-01-22 16:21:24 +00:00
|
|
|
|
|
|
|
return;
|
1999-11-27 22:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t count = (size_t)s_num, n;
|
1999-01-22 16:21:24 +00:00
|
|
|
|
|
|
|
wxArrayThread threads;
|
|
|
|
|
|
|
|
// first create them all...
|
|
|
|
for ( n = 0; n < count; n++ )
|
|
|
|
{
|
1999-10-19 13:58:16 +00:00
|
|
|
wxThread *thr = CreateThread();
|
|
|
|
|
|
|
|
// we want to show the effect of SetPriority(): the first thread will
|
|
|
|
// have the lowest priority, the second - the highest, all the rest
|
|
|
|
// the normal one
|
|
|
|
if ( n == 0 )
|
|
|
|
thr->SetPriority(WXTHREAD_MIN_PRIORITY);
|
|
|
|
else if ( n == 1 )
|
|
|
|
thr->SetPriority(WXTHREAD_MAX_PRIORITY);
|
|
|
|
else
|
|
|
|
thr->SetPriority(WXTHREAD_DEFAULT_PRIORITY);
|
|
|
|
|
|
|
|
threads.Add(thr);
|
1999-01-22 16:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxString msg;
|
2001-09-21 20:21:44 +00:00
|
|
|
msg.Printf(wxT("%d new threads created."), count);
|
1999-01-22 16:21:24 +00:00
|
|
|
SetStatusText(msg, 1);
|
|
|
|
|
|
|
|
// ...and then start them
|
|
|
|
for ( n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
threads[n]->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
MyThread *thread = CreateThread();
|
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
if ( thread->Run() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("Can't start thread!"));
|
1999-01-17 22:39:58 +00:00
|
|
|
}
|
1999-01-22 16:21:24 +00:00
|
|
|
|
2002-12-14 18:13:27 +00:00
|
|
|
SetStatusText(_T("New thread started."), 1);
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
1998-07-27 20:50:48 +00:00
|
|
|
void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
2000-02-23 19:04:31 +00:00
|
|
|
wxGetApp().m_critsect.Enter();
|
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// stop the last thread
|
1999-08-26 18:04:46 +00:00
|
|
|
if ( wxGetApp().m_threads.IsEmpty() )
|
1999-01-09 00:28:27 +00:00
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("No thread to stop!"));
|
2000-02-23 19:04:31 +00:00
|
|
|
|
|
|
|
wxGetApp().m_critsect.Leave();
|
1999-01-09 00:28:27 +00:00
|
|
|
}
|
1999-01-17 22:39:58 +00:00
|
|
|
else
|
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
wxThread *thread = wxGetApp().m_threads.Last();
|
1999-01-22 16:21:24 +00:00
|
|
|
|
|
|
|
// it's important to leave critical section before calling Delete()
|
1999-08-26 18:04:46 +00:00
|
|
|
// because delete will (implicitly) call OnExit() which also tries
|
1999-01-22 16:21:24 +00:00
|
|
|
// to enter the same crit section - would dead lock.
|
1999-08-26 18:04:46 +00:00
|
|
|
wxGetApp().m_critsect.Leave();
|
1999-01-22 16:21:24 +00:00
|
|
|
|
|
|
|
thread->Delete();
|
|
|
|
|
2002-12-14 18:13:27 +00:00
|
|
|
SetStatusText(_T("Thread stopped."), 1);
|
1999-01-17 22:39:58 +00:00
|
|
|
}
|
1998-12-27 00:54:53 +00:00
|
|
|
}
|
1998-06-16 17:17:02 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
1999-01-09 00:28:27 +00:00
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
// resume first suspended thread
|
1999-08-26 18:04:46 +00:00
|
|
|
size_t n = 0, count = wxGetApp().m_threads.Count();
|
|
|
|
while ( n < count && !wxGetApp().m_threads[n]->IsPaused() )
|
1999-01-17 22:39:58 +00:00
|
|
|
n++;
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
if ( n == count )
|
1999-01-22 16:21:24 +00:00
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("No thread to resume!"));
|
1999-01-22 16:21:24 +00:00
|
|
|
}
|
1998-12-27 00:54:53 +00:00
|
|
|
else
|
1999-01-22 16:21:24 +00:00
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
wxGetApp().m_threads[n]->Resume();
|
1999-01-22 16:21:24 +00:00
|
|
|
|
2002-12-14 18:13:27 +00:00
|
|
|
SetStatusText(_T("Thread resumed."), 1);
|
1999-01-22 16:21:24 +00:00
|
|
|
}
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
1998-07-27 20:50:48 +00:00
|
|
|
void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
|
1998-12-26 20:09:03 +00:00
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
1999-01-09 00:28:27 +00:00
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
// pause last running thread
|
1999-08-26 18:04:46 +00:00
|
|
|
int n = wxGetApp().m_threads.Count() - 1;
|
|
|
|
while ( n >= 0 && !wxGetApp().m_threads[n]->IsRunning() )
|
1998-12-27 00:54:53 +00:00
|
|
|
n--;
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
if ( n < 0 )
|
1999-01-22 16:21:24 +00:00
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("No thread to pause!"));
|
1999-01-22 16:21:24 +00:00
|
|
|
}
|
1998-12-27 00:54:53 +00:00
|
|
|
else
|
1999-01-22 16:21:24 +00:00
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
wxGetApp().m_threads[n]->Pause();
|
1999-06-01 15:32:12 +00:00
|
|
|
|
2002-12-14 18:13:27 +00:00
|
|
|
SetStatusText(_T("Thread paused."), 1);
|
1999-01-22 16:21:24 +00:00
|
|
|
}
|
1998-12-26 20:09:03 +00:00
|
|
|
}
|
|
|
|
|
1999-01-02 22:55:03 +00:00
|
|
|
// set the frame title indicating the current number of threads
|
2003-06-23 19:43:27 +00:00
|
|
|
void MyFrame::OnIdle(wxIdleEvent& event)
|
1999-01-02 22:55:03 +00:00
|
|
|
{
|
2002-07-01 14:55:39 +00:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
|
|
|
|
1999-04-11 23:15:31 +00:00
|
|
|
// update the counts of running/total threads
|
1999-01-02 22:55:03 +00:00
|
|
|
size_t nRunning = 0,
|
1999-08-26 18:04:46 +00:00
|
|
|
nCount = wxGetApp().m_threads.Count();
|
1999-01-02 22:55:03 +00:00
|
|
|
for ( size_t n = 0; n < nCount; n++ )
|
|
|
|
{
|
1999-08-26 18:04:46 +00:00
|
|
|
if ( wxGetApp().m_threads[n]->IsRunning() )
|
1999-01-02 22:55:03 +00:00
|
|
|
nRunning++;
|
|
|
|
}
|
|
|
|
|
1999-01-22 16:21:24 +00:00
|
|
|
if ( nCount != m_nCount || nRunning != m_nRunning )
|
|
|
|
{
|
|
|
|
m_nRunning = nRunning;
|
|
|
|
m_nCount = nCount;
|
|
|
|
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogStatus(this, wxT("%u threads total, %u running."), nCount, nRunning);
|
1999-01-22 16:21:24 +00:00
|
|
|
}
|
|
|
|
//else: avoid flicker - don't print anything
|
2003-06-23 19:43:27 +00:00
|
|
|
|
|
|
|
event.Skip();
|
1998-12-26 20:09:03 +00:00
|
|
|
}
|
1998-06-16 17:17:02 +00:00
|
|
|
|
1998-07-27 20:50:48 +00:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
2002-07-01 14:55:39 +00:00
|
|
|
// NB: although the OS will terminate all the threads anyhow when the main
|
|
|
|
// one exits, it's good practice to do it ourselves -- even if it's not
|
|
|
|
// completely trivial in this example
|
|
|
|
|
|
|
|
// tell all the threads to terminate: note that they can't terminate while
|
|
|
|
// we're deleting them because they will block in their OnExit() -- this is
|
|
|
|
// important as otherwise we might access invalid array elements
|
1999-01-17 22:39:58 +00:00
|
|
|
{
|
2002-07-01 14:55:39 +00:00
|
|
|
wxGetApp().m_critsect.Enter();
|
|
|
|
|
|
|
|
// check if we have any threads running first
|
|
|
|
const wxArrayThread& threads = wxGetApp().m_threads;
|
|
|
|
size_t count = threads.GetCount();
|
|
|
|
|
|
|
|
if ( count )
|
|
|
|
{
|
|
|
|
// we do, ask them to stop
|
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
threads[n]->Delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the flag for MyThread::OnExit()
|
|
|
|
wxGetApp().m_waitingUntilAllDone = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxGetApp().m_critsect.Leave();
|
|
|
|
|
|
|
|
if ( count )
|
|
|
|
{
|
|
|
|
// now wait for them to really terminate but leave the GUI mutex
|
|
|
|
// before doing it as otherwise we might dead lock
|
|
|
|
wxMutexGuiLeave();
|
|
|
|
|
|
|
|
wxGetApp().m_condAllDone.Wait();
|
|
|
|
|
|
|
|
wxMutexGuiEnter();
|
|
|
|
}
|
|
|
|
//else: no threads to terminate, no condition to wait for
|
1999-01-17 22:39:58 +00:00
|
|
|
}
|
1999-01-02 22:55:03 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
Close(TRUE);
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
void MyFrame::OnExecMain(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2002-05-10 12:26:30 +00:00
|
|
|
wxLogMessage(wxT("The exit code from the main program is %ld"),
|
2002-12-14 18:13:27 +00:00
|
|
|
EXEC(_T("/bin/echo \"main program\"")));
|
2002-03-27 17:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecThread(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2002-05-10 12:26:30 +00:00
|
|
|
MyExecThread thread(wxT("/bin/echo \"child thread\""));
|
2002-03-27 17:15:13 +00:00
|
|
|
thread.Run();
|
|
|
|
|
2002-05-10 12:26:30 +00:00
|
|
|
wxLogMessage(wxT("The exit code from a child thread is %ld"),
|
2002-03-27 17:15:13 +00:00
|
|
|
(long)thread.Wait());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnShowCPUs(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
int nCPUs = wxThread::GetCPUCount();
|
|
|
|
switch ( nCPUs )
|
|
|
|
{
|
|
|
|
case -1:
|
2002-12-14 18:13:27 +00:00
|
|
|
msg = _T("Unknown number of CPUs");
|
2002-03-27 17:15:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2002-12-14 18:13:27 +00:00
|
|
|
msg = _T("WARNING: you're running without any CPUs!");
|
2002-03-27 17:15:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2002-12-14 18:13:27 +00:00
|
|
|
msg = _T("This system only has one CPU.");
|
2002-03-27 17:15:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-05-10 12:26:30 +00:00
|
|
|
msg.Printf(wxT("This system has %d CPUs"), nCPUs);
|
2002-03-27 17:15:13 +00:00
|
|
|
}
|
2003-03-23 20:14:19 +00:00
|
|
|
|
2002-03-27 17:15:13 +00:00
|
|
|
wxLogMessage(msg);
|
|
|
|
}
|
|
|
|
|
1998-07-27 20:50:48 +00:00
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 17:17:02 +00:00
|
|
|
{
|
2003-03-23 20:14:19 +00:00
|
|
|
wxMessageDialog dialog(this,
|
2002-12-14 18:13:27 +00:00
|
|
|
_T("wxWindows multithreaded application sample\n")
|
|
|
|
_T("(c) 1998 Julian Smart, Guilhem Lavaux\n")
|
|
|
|
_T("(c) 1999 Vadim Zeitlin\n")
|
|
|
|
_T("(c) 2000 Robert Roebling"),
|
|
|
|
_T("About wxThread sample"),
|
1999-01-02 22:55:03 +00:00
|
|
|
wxOK | wxICON_INFORMATION);
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
dialog.ShowModal();
|
1998-06-16 17:17:02 +00:00
|
|
|
}
|
|
|
|
|
1999-01-09 00:28:27 +00:00
|
|
|
void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
m_txtctrl->Clear();
|
|
|
|
}
|
1999-12-30 12:02:57 +00:00
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
void MyFrame::OnUpdateWorker(wxUpdateUIEvent& event)
|
|
|
|
{
|
|
|
|
event.Enable( m_dlgProgress == NULL );
|
|
|
|
}
|
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
MyWorkerThread *thread = new MyWorkerThread(this);
|
|
|
|
|
|
|
|
if ( thread->Create() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("Can't create thread!"));
|
1999-12-30 12:02:57 +00:00
|
|
|
}
|
2000-01-29 02:34:46 +00:00
|
|
|
|
|
|
|
m_dlgProgress = new wxProgressDialog
|
|
|
|
(
|
2002-12-14 18:13:27 +00:00
|
|
|
_T("Progress dialog"),
|
|
|
|
_T("Wait until the thread terminates or press [Cancel]"),
|
2000-01-29 02:34:46 +00:00
|
|
|
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;
|
|
|
|
|
1999-12-30 12:02:57 +00:00
|
|
|
thread->Run();
|
|
|
|
}
|
|
|
|
|
2000-01-02 16:25:28 +00:00
|
|
|
void MyFrame::OnWorkerEvent(wxCommandEvent& event)
|
1999-12-30 12:02:57 +00:00
|
|
|
{
|
2000-01-29 02:34:46 +00:00
|
|
|
#if 0
|
2002-12-14 18:13:27 +00:00
|
|
|
WriteText( _T("Got message from worker thread: ") );
|
2000-01-02 16:25:28 +00:00
|
|
|
WriteText( event.GetString() );
|
2002-12-14 18:13:27 +00:00
|
|
|
WriteText( _T("\n") );
|
2000-01-29 02:34:46 +00:00
|
|
|
#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
|
1999-12-30 12:02:57 +00:00
|
|
|
}
|
|
|
|
|
2000-01-29 02:34:46 +00:00
|
|
|
bool MyFrame::Cancelled()
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker lock(m_critsectWork);
|
|
|
|
|
|
|
|
return m_cancelled;
|
|
|
|
}
|