1998-06-16 13:17:02 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2000-03-14 14:35:40 -05:00
|
|
|
// Name: thread.cpp
|
2004-05-25 07:20:37 -04:00
|
|
|
// Purpose: wxWidgets thread sample
|
2002-07-01 10:55:39 -04:00
|
|
|
// Author: Guilhem Lavaux, Vadim Zeitlin
|
1998-06-16 13:17:02 -04:00
|
|
|
// Modified by:
|
|
|
|
// Created: 06/16/98
|
|
|
|
// RCS-ID: $Id$
|
2004-05-25 07:20:37 -04:00
|
|
|
// Copyright: (c) 1998-2002 wxWidgets team
|
1999-01-02 17:55:03 -05:00
|
|
|
// Licence: wxWindows license
|
1998-06-16 13:17:02 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
1999-01-02 17:55:03 -05:00
|
|
|
#pragma hdrstop
|
1998-06-16 13:17:02 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
1999-01-02 17:55:03 -05:00
|
|
|
#include "wx/wx.h"
|
1998-06-16 13:17:02 -04:00
|
|
|
#endif
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
#if !wxUSE_THREADS
|
|
|
|
#error "This sample requires thread support!"
|
|
|
|
#endif // wxUSE_THREADS
|
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
#include "wx/thread.h"
|
|
|
|
#include "wx/dynarray.h"
|
2004-02-29 10:21:11 -05:00
|
|
|
#include "wx/numdlg.h"
|
1998-06-16 13:17:02 -04:00
|
|
|
|
2000-01-28 21:34:46 -05:00
|
|
|
#include "wx/progdlg.h"
|
|
|
|
|
2005-03-18 05:05:27 -05:00
|
|
|
#include "../sample.xpm"
|
|
|
|
|
2002-07-01 10:55:39 -04:00
|
|
|
// define this to use wxExecute in the exec tests, otherwise just use system
|
2002-04-12 15:05:11 -04:00
|
|
|
#define USE_EXECUTE
|
2002-07-01 10:55:39 -04:00
|
|
|
|
2002-03-27 12:15:13 -05:00
|
|
|
#ifdef USE_EXECUTE
|
|
|
|
#define EXEC(cmd) wxExecute((cmd), wxEXEC_SYNC)
|
|
|
|
#else
|
|
|
|
#define EXEC(cmd) system(cmd)
|
|
|
|
#endif
|
2000-02-23 14:04:31 -05:00
|
|
|
|
1999-08-26 14:04:46 -04:00
|
|
|
class MyThread;
|
2003-10-23 17:38:31 -04:00
|
|
|
WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
|
1999-08-26 14:04:46 -04:00
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
// Define a new application type
|
1999-01-17 17:39:58 -05:00
|
|
|
class MyApp : public wxApp
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
1999-10-19 09:58:16 -04:00
|
|
|
public:
|
2002-07-01 10:55:39 -04:00
|
|
|
MyApp();
|
2004-10-06 16:54:57 -04:00
|
|
|
virtual ~MyApp(){};
|
2002-07-01 10:55:39 -04:00
|
|
|
|
1999-10-19 09:58:16 -04: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 10:55:39 -04:00
|
|
|
|
2003-09-21 16:36:07 -04:00
|
|
|
// semaphore used to wait for the threads to exit, see MyFrame::OnQuit()
|
|
|
|
wxSemaphore m_semAllDone;
|
2002-07-01 10:55:39 -04:00
|
|
|
|
2003-09-21 16:36:07 -04:00
|
|
|
// the last exiting thread should post to m_semAllDone if this is true
|
2002-07-01 10:55:39 -04:00
|
|
|
// (protected by the same m_critsect)
|
|
|
|
bool m_waitingUntilAllDone;
|
1998-06-16 13:17:02 -04:00
|
|
|
};
|
|
|
|
|
1999-08-26 14:04:46 -04:00
|
|
|
// Create a new application object
|
1999-10-19 09:58:16 -04:00
|
|
|
IMPLEMENT_APP(MyApp)
|
1998-06-16 13:17:02 -04:00
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
public:
|
|
|
|
// ctor
|
1999-01-17 17:39:58 -05:00
|
|
|
MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h);
|
2003-09-21 19:54:47 -04:00
|
|
|
virtual ~MyFrame();
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// operations
|
|
|
|
void WriteText(const wxString& text) { m_txtctrl->WriteText(text); }
|
|
|
|
|
2000-01-28 21:34:46 -05:00
|
|
|
// accessors for MyWorkerThread (called in its context!)
|
|
|
|
bool Cancelled();
|
|
|
|
|
2002-07-01 10:55:39 -04:00
|
|
|
protected:
|
1998-12-26 19:54:53 -05:00
|
|
|
// callbacks
|
1998-06-16 13:17:02 -04:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
1999-01-08 19:28:27 -05:00
|
|
|
void OnClear(wxCommandEvent& event);
|
1998-12-26 19:54:53 -05:00
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
void OnStartThread(wxCommandEvent& event);
|
1999-01-22 11:21:24 -05:00
|
|
|
void OnStartThreads(wxCommandEvent& event);
|
1998-06-16 13:17:02 -04:00
|
|
|
void OnStopThread(wxCommandEvent& event);
|
|
|
|
void OnPauseThread(wxCommandEvent& event);
|
1998-12-26 19:54:53 -05:00
|
|
|
void OnResumeThread(wxCommandEvent& event);
|
2000-01-28 21:34:46 -05:00
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
void OnStartWorker(wxCommandEvent& event);
|
|
|
|
void OnWorkerEvent(wxCommandEvent& event);
|
2000-01-28 21:34:46 -05:00
|
|
|
void OnUpdateWorker(wxUpdateUIEvent& event);
|
1998-12-26 19:54:53 -05:00
|
|
|
|
2002-03-27 12:15:13 -05:00
|
|
|
void OnExecMain(wxCommandEvent& event);
|
|
|
|
void OnExecThread(wxCommandEvent& event);
|
|
|
|
|
|
|
|
void OnShowCPUs(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
void OnIdle(wxIdleEvent &event);
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
private:
|
1999-01-22 11:21:24 -05:00
|
|
|
// helper function - creates a new thread (but doesn't run it)
|
|
|
|
MyThread *CreateThread();
|
1999-06-01 11:32:12 -04:00
|
|
|
|
1999-01-17 17:39:58 -05:00
|
|
|
// just some place to put our messages in
|
|
|
|
wxTextCtrl *m_txtctrl;
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
// remember the number of running threads and total number of threads
|
|
|
|
size_t m_nRunning, m_nCount;
|
|
|
|
|
2000-01-28 21:34:46 -05: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-26 19:54:53 -05:00
|
|
|
DECLARE_EVENT_TABLE()
|
1998-06-16 13:17:02 -04:00
|
|
|
};
|
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
// ID for the menu commands
|
|
|
|
enum
|
|
|
|
{
|
2005-06-02 08:04:48 -04:00
|
|
|
THREAD_QUIT = wxID_EXIT,
|
|
|
|
THREAD_ABOUT = wxID_ABOUT,
|
2002-03-27 12:15:13 -05:00
|
|
|
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,
|
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
WORKER_EVENT // this one gets sent from the worker thread
|
|
|
|
};
|
|
|
|
|
2002-03-27 12:15:13 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 07:02:57 -05:00
|
|
|
// GUI thread
|
2002-03-27 12:15:13 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 07:02:57 -05:00
|
|
|
|
1999-01-08 19:28:27 -05:00
|
|
|
class MyThread : public wxThread
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
public:
|
1998-06-16 13:17:02 -04:00
|
|
|
MyThread(MyFrame *frame);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
|
|
|
// thread execution starts here
|
|
|
|
virtual void *Entry();
|
|
|
|
|
1999-01-17 17:39:58 -05:00
|
|
|
// called when the thread exits - whether it terminates normally or is
|
|
|
|
// stopped with Delete() (but not when it is Kill()ed!)
|
1999-01-08 19:28:27 -05:00
|
|
|
virtual void OnExit();
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
// write something to the text control
|
|
|
|
void WriteText(const wxString& text);
|
1998-12-26 19:54:53 -05:00
|
|
|
|
|
|
|
public:
|
2006-10-02 01:36:31 -04:00
|
|
|
unsigned m_count;
|
1998-06-16 13:17:02 -04:00
|
|
|
MyFrame *m_frame;
|
|
|
|
};
|
|
|
|
|
|
|
|
MyThread::MyThread(MyFrame *frame)
|
1998-12-26 19:54:53 -05:00
|
|
|
: wxThread()
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
m_count = 0;
|
|
|
|
m_frame = frame;
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
1999-01-02 17:55:03 -05: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 11:32:12 -04:00
|
|
|
|
1999-05-22 04:01:17 -04:00
|
|
|
wxMutexGuiEnter();
|
1999-06-01 11:32:12 -04:00
|
|
|
|
1999-11-22 14:44:25 -05:00
|
|
|
msg << text;
|
1999-01-02 17:55:03 -05:00
|
|
|
m_frame->WriteText(msg);
|
2000-01-28 21:34:46 -05:00
|
|
|
|
1999-05-22 04:01:17 -04:00
|
|
|
wxMutexGuiLeave();
|
1999-01-08 19:28:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyThread::OnExit()
|
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxCriticalSectionLocker locker(wxGetApp().m_critsect);
|
|
|
|
|
2002-07-01 10:55:39 -04: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 )
|
|
|
|
{
|
2004-06-01 03:45:11 -04:00
|
|
|
wxGetApp().m_waitingUntilAllDone = false;
|
2002-07-01 10:55:39 -04:00
|
|
|
|
2003-09-21 16:36:07 -04:00
|
|
|
wxGetApp().m_semAllDone.Post();
|
2002-07-01 10:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
1999-01-02 17:55:03 -05:00
|
|
|
}
|
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
void *MyThread::Entry()
|
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
wxString text;
|
1999-01-02 17:55:03 -05: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 05:22:07 -05:00
|
|
|
text.Printf(wxT("Thread 0x%lx started (priority = %u).\n"),
|
1999-11-27 17:57:06 -05:00
|
|
|
GetId(), GetPriority());
|
1999-01-02 17:55:03 -05:00
|
|
|
WriteText(text);
|
2000-02-04 15:27:10 -05:00
|
|
|
// wxLogMessage(text); -- test wxLog thread safeness
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-01-08 19:28:27 -05:00
|
|
|
for ( m_count = 0; m_count < 10; m_count++ )
|
1999-01-02 17:55:03 -05: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 05:22:07 -05:00
|
|
|
text.Printf(wxT("[%u] Thread 0x%lx here.\n"), m_count, GetId());
|
1999-01-02 17:55:03 -05:00
|
|
|
WriteText(text);
|
1998-12-26 19:54:53 -05:00
|
|
|
|
1999-01-17 17:39:58 -05:00
|
|
|
// wxSleep() can't be called from non-GUI thread!
|
|
|
|
wxThread::Sleep(1000);
|
1998-12-26 19:54:53 -05:00
|
|
|
}
|
1999-01-02 17:55:03 -05: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 05:22:07 -05:00
|
|
|
text.Printf(wxT("Thread 0x%lx finished.\n"), GetId());
|
1999-01-02 17:55:03 -05:00
|
|
|
WriteText(text);
|
2000-02-04 15:27:10 -05:00
|
|
|
// wxLogMessage(text); -- test wxLog thread safeness
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
return NULL;
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
2002-03-27 12:15:13 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 07:02:57 -05:00
|
|
|
// worker thread
|
2002-03-27 12:15:13 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-30 07:02:57 -05:00
|
|
|
|
|
|
|
class MyWorkerThread : public wxThread
|
1999-01-02 17:55:03 -05:00
|
|
|
{
|
1999-12-30 07:02:57 -05: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;
|
2006-10-02 01:36:31 -04:00
|
|
|
unsigned m_count;
|
1999-01-02 17:55:03 -05:00
|
|
|
};
|
1998-06-16 13:17:02 -04:00
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
MyWorkerThread::MyWorkerThread(MyFrame *frame)
|
|
|
|
: wxThread()
|
|
|
|
{
|
|
|
|
m_frame = frame;
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyWorkerThread::OnExit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MyWorkerThread::Entry()
|
|
|
|
{
|
2000-01-28 21:34:46 -05:00
|
|
|
for ( m_count = 0; !m_frame->Cancelled() && (m_count < 100); m_count++ )
|
1999-12-30 07:02:57 -05:00
|
|
|
{
|
|
|
|
// check if we were asked to exit
|
|
|
|
if ( TestDestroy() )
|
|
|
|
break;
|
2000-01-28 21:34:46 -05:00
|
|
|
|
|
|
|
// create any type of command event here
|
1999-12-30 07:02:57 -05:00
|
|
|
wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
|
2000-01-28 21:34:46 -05:00
|
|
|
event.SetInt( m_count );
|
|
|
|
|
2000-01-02 11:25:28 -05:00
|
|
|
// send in a thread-safe way
|
1999-12-30 07:02:57 -05:00
|
|
|
wxPostEvent( m_frame, event );
|
2000-01-28 21:34:46 -05:00
|
|
|
|
2000-01-02 11:25:28 -05:00
|
|
|
// wxSleep() can't be called from non-main thread!
|
2000-01-28 21:34:46 -05:00
|
|
|
wxThread::Sleep(200);
|
1999-12-30 07:02:57 -05:00
|
|
|
}
|
|
|
|
|
2000-01-28 21:34:46 -05:00
|
|
|
wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
|
|
|
|
event.SetInt(-1); // that's all
|
|
|
|
wxPostEvent( m_frame, event );
|
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-03-27 12:15:13 -05: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 07:02:57 -05:00
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2002-03-27 12:15:13 -05: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 07:02:57 -05:00
|
|
|
EVT_MENU(WORKER_EVENT, MyFrame::OnWorkerEvent)
|
1998-12-26 19:54:53 -05:00
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
EVT_IDLE(MyFrame::OnIdle)
|
1998-06-16 13:17:02 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2002-07-01 10:55:39 -04:00
|
|
|
MyApp::MyApp()
|
2003-09-21 16:36:07 -04:00
|
|
|
: m_semAllDone()
|
2002-07-01 10:55:39 -04:00
|
|
|
{
|
2004-06-01 03:45:11 -04:00
|
|
|
m_waitingUntilAllDone = false;
|
2002-07-01 10:55:39 -04:00
|
|
|
}
|
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
1999-01-02 17:55:03 -05:00
|
|
|
bool MyApp::OnInit()
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
2007-02-03 19:34:18 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2002-03-27 12:15:13 -05: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 14:04:31 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// Create the main frame window
|
2004-05-25 07:20:37 -04:00
|
|
|
MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWidgets threads sample"),
|
1999-01-08 19:28:27 -05:00
|
|
|
50, 50, 450, 340);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// Make a menubar
|
2002-03-27 12:15:13 -05:00
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
|
|
|
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
2002-12-14 13:13:27 -05:00
|
|
|
menuFile->Append(THREAD_CLEAR, _T("&Clear log\tCtrl-L"));
|
2002-03-27 12:15:13 -05:00
|
|
|
menuFile->AppendSeparator();
|
2002-12-14 13:13:27 -05:00
|
|
|
menuFile->Append(THREAD_QUIT, _T("E&xit\tAlt-X"));
|
|
|
|
menuBar->Append(menuFile, _T("&File"));
|
2002-03-27 12:15:13 -05:00
|
|
|
|
|
|
|
wxMenu *menuThread = new wxMenu;
|
2002-12-14 13:13:27 -05: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 12:15:13 -05:00
|
|
|
menuThread->AppendSeparator();
|
2002-12-14 13:13:27 -05: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 12:15:13 -05:00
|
|
|
menuThread->AppendSeparator();
|
2002-12-14 13:13:27 -05:00
|
|
|
menuThread->Append(THREAD_START_WORKER, _T("Start &worker thread\tCtrl-W"));
|
|
|
|
menuBar->Append(menuThread, _T("&Thread"));
|
2002-03-27 12:15:13 -05:00
|
|
|
|
|
|
|
wxMenu *menuExec = new wxMenu;
|
2002-12-14 13:13:27 -05: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 12:15:13 -05:00
|
|
|
|
|
|
|
wxMenu *menuHelp = new wxMenu;
|
2002-12-14 13:13:27 -05:00
|
|
|
menuHelp->Append(THREAD_SHOWCPUS, _T("&Show CPU count"));
|
2002-03-27 12:15:13 -05:00
|
|
|
menuHelp->AppendSeparator();
|
2002-12-14 13:13:27 -05:00
|
|
|
menuHelp->Append(THREAD_ABOUT, _T("&About..."));
|
|
|
|
menuBar->Append(menuHelp, _T("&Help"));
|
2002-03-27 12:15:13 -05:00
|
|
|
|
|
|
|
frame->SetMenuBar(menuBar);
|
2005-03-18 05:05:27 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// Show the frame
|
2004-06-01 03:45:11 -04:00
|
|
|
frame->Show(true);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
SetTopWindow(frame);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
2004-06-01 03:45:11 -04:00
|
|
|
return true;
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// My frame constructor
|
1999-01-17 17:39:58 -05:00
|
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title,
|
|
|
|
int x, int y, int w, int h)
|
2004-06-01 03:45:11 -04:00
|
|
|
: wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
2005-03-18 05:05:27 -05:00
|
|
|
SetIcon(wxIcon(sample_xpm));
|
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
m_nRunning = m_nCount = 0;
|
|
|
|
|
2000-01-28 21:34:46 -05:00
|
|
|
m_dlgProgress = (wxProgressDialog *)NULL;
|
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
1999-01-22 11:21:24 -05:00
|
|
|
CreateStatusBar(2);
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-01-02 17:55:03 -05:00
|
|
|
|
2004-06-01 03:45:11 -04:00
|
|
|
m_txtctrl = new wxTextCtrl(this, wxID_ANY, _T(""), wxPoint(0, 0), wxSize(0, 0),
|
1999-01-08 19:28:27 -05:00
|
|
|
wxTE_MULTILINE | wxTE_READONLY);
|
1998-06-16 13:17:02 -04:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
}
|
1998-06-16 13:17:02 -04:00
|
|
|
|
2003-09-21 19:54:47 -04:00
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
wxThread *thread;
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
// set the flag for MyThread::OnExit()
|
2004-06-01 03:45:11 -04:00
|
|
|
wxGetApp().m_waitingUntilAllDone = true;
|
2003-09-21 19:54:47 -04:00
|
|
|
|
|
|
|
// stop all threads
|
|
|
|
while ( ! threads.IsEmpty() )
|
|
|
|
{
|
|
|
|
thread = threads.Last();
|
|
|
|
|
|
|
|
wxGetApp().m_critsect.Leave();
|
|
|
|
|
|
|
|
thread->Delete();
|
|
|
|
|
|
|
|
wxGetApp().m_critsect.Enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxGetApp().m_critsect.Leave();
|
|
|
|
|
|
|
|
if ( count )
|
|
|
|
{
|
|
|
|
// now wait for them to really terminate
|
|
|
|
wxGetApp().m_semAllDone.Wait();
|
|
|
|
}
|
|
|
|
//else: no threads to terminate, no condition to wait for
|
|
|
|
}
|
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
MyThread *MyFrame::CreateThread()
|
1998-12-26 19:54:53 -05:00
|
|
|
{
|
|
|
|
MyThread *thread = new MyThread(this);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-01-17 17:39:58 -05:00
|
|
|
if ( thread->Create() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
wxLogError(wxT("Can't create thread!"));
|
1999-01-17 17:39:58 -05:00
|
|
|
}
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-08-26 14:04:46 -04:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
|
|
|
wxGetApp().m_threads.Add(thread);
|
1999-01-17 17:39:58 -05:00
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
2006-10-02 01:36:31 -04:00
|
|
|
static long s_num;
|
1999-11-27 17:57:06 -05:00
|
|
|
|
2002-12-14 13:13:27 -05:00
|
|
|
s_num = wxGetNumberFromUser(_T("How many threads to start: "), _T(""),
|
|
|
|
_T("wxThread sample"), s_num, 1, 10000, this);
|
1999-11-27 17:57:06 -05:00
|
|
|
if ( s_num == -1 )
|
|
|
|
{
|
|
|
|
s_num = 10;
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
return;
|
1999-11-27 17:57:06 -05:00
|
|
|
}
|
|
|
|
|
2006-10-02 01:36:31 -04:00
|
|
|
unsigned count = unsigned(s_num), n;
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
wxArrayThread threads;
|
|
|
|
|
|
|
|
// first create them all...
|
|
|
|
for ( n = 0; n < count; n++ )
|
|
|
|
{
|
1999-10-19 09:58:16 -04: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 11:21:24 -05:00
|
|
|
}
|
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
1999-01-22 11:21:24 -05:00
|
|
|
wxString msg;
|
2001-09-21 16:21:44 -04:00
|
|
|
msg.Printf(wxT("%d new threads created."), count);
|
1999-01-22 11:21:24 -05:00
|
|
|
SetStatusText(msg, 1);
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
// ...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 17:39:58 -05:00
|
|
|
if ( thread->Run() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
wxLogError(wxT("Can't start thread!"));
|
1999-01-17 17:39:58 -05:00
|
|
|
}
|
1999-01-22 11:21:24 -05:00
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
2002-12-14 13:13:27 -05:00
|
|
|
SetStatusText(_T("New thread started."), 1);
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
1998-07-27 16:50:48 -04:00
|
|
|
void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
2000-02-23 14:04:31 -05:00
|
|
|
wxGetApp().m_critsect.Enter();
|
|
|
|
|
1999-01-17 17:39:58 -05:00
|
|
|
// stop the last thread
|
1999-08-26 14:04:46 -04:00
|
|
|
if ( wxGetApp().m_threads.IsEmpty() )
|
1999-01-08 19:28:27 -05:00
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
wxLogError(wxT("No thread to stop!"));
|
2000-02-23 14:04:31 -05:00
|
|
|
|
|
|
|
wxGetApp().m_critsect.Leave();
|
1999-01-08 19:28:27 -05:00
|
|
|
}
|
1999-01-17 17:39:58 -05:00
|
|
|
else
|
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxThread *thread = wxGetApp().m_threads.Last();
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
// it's important to leave critical section before calling Delete()
|
1999-08-26 14:04:46 -04:00
|
|
|
// because delete will (implicitly) call OnExit() which also tries
|
1999-01-22 11:21:24 -05:00
|
|
|
// to enter the same crit section - would dead lock.
|
1999-08-26 14:04:46 -04:00
|
|
|
wxGetApp().m_critsect.Leave();
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
thread->Delete();
|
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
2002-12-14 13:13:27 -05:00
|
|
|
SetStatusText(_T("Thread stopped."), 1);
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-01-17 17:39:58 -05:00
|
|
|
}
|
1998-12-26 19:54:53 -05:00
|
|
|
}
|
1998-06-16 13:17:02 -04:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
1999-01-08 19:28:27 -05:00
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
// resume first suspended thread
|
1999-08-26 14:04:46 -04:00
|
|
|
size_t n = 0, count = wxGetApp().m_threads.Count();
|
|
|
|
while ( n < count && !wxGetApp().m_threads[n]->IsPaused() )
|
1999-01-17 17:39:58 -05:00
|
|
|
n++;
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-01-17 17:39:58 -05:00
|
|
|
if ( n == count )
|
1999-01-22 11:21:24 -05:00
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
wxLogError(wxT("No thread to resume!"));
|
1999-01-22 11:21:24 -05:00
|
|
|
}
|
1998-12-26 19:54:53 -05:00
|
|
|
else
|
1999-01-22 11:21:24 -05:00
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxGetApp().m_threads[n]->Resume();
|
1999-01-22 11:21:24 -05:00
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
2002-12-14 13:13:27 -05:00
|
|
|
SetStatusText(_T("Thread resumed."), 1);
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-01-22 11:21:24 -05:00
|
|
|
}
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
1998-07-27 16:50:48 -04:00
|
|
|
void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
|
1998-12-26 15:09:03 -05:00
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
1999-01-08 19:28:27 -05:00
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
// pause last running thread
|
1999-08-26 14:04:46 -04:00
|
|
|
int n = wxGetApp().m_threads.Count() - 1;
|
|
|
|
while ( n >= 0 && !wxGetApp().m_threads[n]->IsRunning() )
|
1998-12-26 19:54:53 -05:00
|
|
|
n--;
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
if ( n < 0 )
|
1999-01-22 11:21:24 -05:00
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
wxLogError(wxT("No thread to pause!"));
|
1999-01-22 11:21:24 -05:00
|
|
|
}
|
1998-12-26 19:54:53 -05:00
|
|
|
else
|
1999-01-22 11:21:24 -05:00
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxGetApp().m_threads[n]->Pause();
|
1999-06-01 11:32:12 -04:00
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
2002-12-14 13:13:27 -05:00
|
|
|
SetStatusText(_T("Thread paused."), 1);
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-01-22 11:21:24 -05:00
|
|
|
}
|
1998-12-26 15:09:03 -05:00
|
|
|
}
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
// set the frame title indicating the current number of threads
|
2003-06-23 15:43:27 -04:00
|
|
|
void MyFrame::OnIdle(wxIdleEvent& event)
|
1999-01-02 17:55:03 -05:00
|
|
|
{
|
2002-07-01 10:55:39 -04:00
|
|
|
wxCriticalSectionLocker enter(wxGetApp().m_critsect);
|
|
|
|
|
1999-04-11 19:15:31 -04:00
|
|
|
// update the counts of running/total threads
|
1999-01-02 17:55:03 -05:00
|
|
|
size_t nRunning = 0,
|
1999-08-26 14:04:46 -04:00
|
|
|
nCount = wxGetApp().m_threads.Count();
|
1999-01-02 17:55:03 -05:00
|
|
|
for ( size_t n = 0; n < nCount; n++ )
|
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
if ( wxGetApp().m_threads[n]->IsRunning() )
|
1999-01-02 17:55:03 -05:00
|
|
|
nRunning++;
|
|
|
|
}
|
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
if ( nCount != m_nCount || nRunning != m_nRunning )
|
|
|
|
{
|
|
|
|
m_nRunning = nRunning;
|
|
|
|
m_nCount = nCount;
|
|
|
|
|
2006-10-02 01:36:31 -04:00
|
|
|
wxLogStatus(this, wxT("%u threads total, %u running."), unsigned(nCount), unsigned(nRunning));
|
1999-01-22 11:21:24 -05:00
|
|
|
}
|
|
|
|
//else: avoid flicker - don't print anything
|
2003-06-23 15:43:27 -04:00
|
|
|
|
|
|
|
event.Skip();
|
1998-12-26 15:09:03 -05:00
|
|
|
}
|
1998-06-16 13:17:02 -04:00
|
|
|
|
1998-07-27 16:50:48 -04:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
2004-06-01 03:45:11 -04:00
|
|
|
Close(true);
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
2002-03-27 12:15:13 -05:00
|
|
|
void MyFrame::OnExecMain(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2002-05-10 08:26:30 -04:00
|
|
|
wxLogMessage(wxT("The exit code from the main program is %ld"),
|
2002-12-14 13:13:27 -05:00
|
|
|
EXEC(_T("/bin/echo \"main program\"")));
|
2002-03-27 12:15:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecThread(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2002-05-10 08:26:30 -04:00
|
|
|
MyExecThread thread(wxT("/bin/echo \"child thread\""));
|
2002-03-27 12:15:13 -05:00
|
|
|
thread.Run();
|
|
|
|
|
2002-05-10 08:26:30 -04:00
|
|
|
wxLogMessage(wxT("The exit code from a child thread is %ld"),
|
2002-03-27 12:15:13 -05:00
|
|
|
(long)thread.Wait());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnShowCPUs(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
int nCPUs = wxThread::GetCPUCount();
|
|
|
|
switch ( nCPUs )
|
|
|
|
{
|
|
|
|
case -1:
|
2002-12-14 13:13:27 -05:00
|
|
|
msg = _T("Unknown number of CPUs");
|
2002-03-27 12:15:13 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2002-12-14 13:13:27 -05:00
|
|
|
msg = _T("WARNING: you're running without any CPUs!");
|
2002-03-27 12:15:13 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2002-12-14 13:13:27 -05:00
|
|
|
msg = _T("This system only has one CPU.");
|
2002-03-27 12:15:13 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-05-10 08:26:30 -04:00
|
|
|
msg.Printf(wxT("This system has %d CPUs"), nCPUs);
|
2002-03-27 12:15:13 -05:00
|
|
|
}
|
2003-03-23 15:14:19 -05:00
|
|
|
|
2002-03-27 12:15:13 -05:00
|
|
|
wxLogMessage(msg);
|
|
|
|
}
|
|
|
|
|
1998-07-27 16:50:48 -04:00
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
2003-03-23 15:14:19 -05:00
|
|
|
wxMessageDialog dialog(this,
|
2004-05-25 07:20:37 -04:00
|
|
|
_T("wxWidgets multithreaded application sample\n")
|
2002-12-14 13:13:27 -05:00
|
|
|
_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 17:55:03 -05:00
|
|
|
wxOK | wxICON_INFORMATION);
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
dialog.ShowModal();
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
1999-01-08 19:28:27 -05:00
|
|
|
void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
m_txtctrl->Clear();
|
|
|
|
}
|
1999-12-30 07:02:57 -05:00
|
|
|
|
2000-01-28 21:34:46 -05:00
|
|
|
void MyFrame::OnUpdateWorker(wxUpdateUIEvent& event)
|
|
|
|
{
|
|
|
|
event.Enable( m_dlgProgress == NULL );
|
|
|
|
}
|
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
MyWorkerThread *thread = new MyWorkerThread(this);
|
|
|
|
|
|
|
|
if ( thread->Create() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
wxLogError(wxT("Can't create thread!"));
|
2005-01-29 15:49:14 -05:00
|
|
|
return;
|
1999-12-30 07:02:57 -05:00
|
|
|
}
|
2000-01-28 21:34:46 -05:00
|
|
|
|
|
|
|
m_dlgProgress = new wxProgressDialog
|
|
|
|
(
|
2002-12-14 13:13:27 -05:00
|
|
|
_T("Progress dialog"),
|
|
|
|
_T("Wait until the thread terminates or press [Cancel]"),
|
2000-01-28 21:34:46 -05: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
|
2004-06-01 03:45:11 -04:00
|
|
|
m_cancelled = false;
|
2000-01-28 21:34:46 -05:00
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
thread->Run();
|
|
|
|
}
|
|
|
|
|
2000-01-02 11:25:28 -05:00
|
|
|
void MyFrame::OnWorkerEvent(wxCommandEvent& event)
|
1999-12-30 07:02:57 -05:00
|
|
|
{
|
2000-01-28 21:34:46 -05:00
|
|
|
#if 0
|
2002-12-14 13:13:27 -05:00
|
|
|
WriteText( _T("Got message from worker thread: ") );
|
2000-01-02 11:25:28 -05:00
|
|
|
WriteText( event.GetString() );
|
2002-12-14 13:13:27 -05:00
|
|
|
WriteText( _T("\n") );
|
2000-01-28 21:34:46 -05: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);
|
|
|
|
|
2004-06-01 03:45:11 -04:00
|
|
|
m_cancelled = true;
|
2000-01-28 21:34:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-12-30 07:02:57 -05:00
|
|
|
}
|
|
|
|
|
2000-01-28 21:34:46 -05:00
|
|
|
bool MyFrame::Cancelled()
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker lock(m_critsectWork);
|
|
|
|
|
|
|
|
return m_cancelled;
|
|
|
|
}
|