1998-06-16 13:17:02 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: test.cpp
|
|
|
|
// Purpose: wxWindows thread sample
|
|
|
|
// Author: Julian Smart(minimal)/Guilhem Lavaux(thread test)
|
|
|
|
// Modified by:
|
|
|
|
// Created: 06/16/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux
|
1999-01-02 17:55:03 -05:00
|
|
|
// Licence: wxWindows license
|
1998-06-16 13:17:02 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-01-08 19:28:27 -05:00
|
|
|
/*
|
1999-10-19 09:58:16 -04:00
|
|
|
TODO: use worker threads to update progress controls instead of writing
|
|
|
|
messages - it will be more visual
|
1999-01-08 19:28:27 -05:00
|
|
|
*/
|
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
#ifdef __GNUG__
|
1999-01-08 19:28:27 -05:00
|
|
|
#pragma implementation "test.cpp"
|
|
|
|
#pragma interface "test.cpp"
|
1998-06-16 13:17:02 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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"
|
1999-01-02 17:55:03 -05:00
|
|
|
#include "wx/time.h"
|
1998-06-16 13:17:02 -04:00
|
|
|
|
1999-08-26 14:04:46 -04:00
|
|
|
class MyThread;
|
|
|
|
WX_DEFINE_ARRAY(wxThread *, wxArrayThread);
|
|
|
|
|
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:
|
|
|
|
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;
|
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);
|
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); }
|
|
|
|
|
|
|
|
// callbacks
|
1998-06-16 13:17:02 -04:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(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);
|
1999-12-30 07:02:57 -05:00
|
|
|
|
|
|
|
void OnStartWorker(wxCommandEvent& event);
|
|
|
|
void OnWorkerEvent(wxCommandEvent& event);
|
1998-12-26 19:54:53 -05:00
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
TEST_QUIT = 1,
|
|
|
|
TEST_TEXT = 101,
|
|
|
|
TEST_ABOUT,
|
|
|
|
TEST_CLEAR,
|
|
|
|
TEST_START_THREAD = 201,
|
|
|
|
TEST_START_THREADS,
|
|
|
|
TEST_STOP_THREAD,
|
|
|
|
TEST_PAUSE_THREAD,
|
|
|
|
TEST_RESUME_THREAD,
|
|
|
|
TEST_START_WORKER,
|
|
|
|
WORKER_EVENT // this one gets sent from the worker thread
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// GUI thread
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
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:
|
|
|
|
size_t 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-08 19:28:27 -05:00
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
m_frame->WriteText(msg);
|
1999-12-29 12:16:55 -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);
|
|
|
|
|
|
|
|
wxGetApp().m_threads.Remove(this);
|
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
|
|
|
|
1999-10-19 09:58:16 -04:00
|
|
|
text.Printf("Thread 0x%x started (priority = %d).\n",
|
1999-11-27 17:57:06 -05:00
|
|
|
GetId(), GetPriority());
|
1999-01-02 17:55:03 -05:00
|
|
|
WriteText(text);
|
|
|
|
|
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;
|
|
|
|
|
1999-11-27 17:57:06 -05:00
|
|
|
text.Printf("[%u] Thread 0x%x 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
|
|
|
|
1999-11-27 17:57:06 -05:00
|
|
|
text.Printf("Thread 0x%x finished.\n", GetId());
|
1999-01-02 17:55:03 -05:00
|
|
|
WriteText(text);
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
return NULL;
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
1999-12-30 07:02:57 -05:00
|
|
|
//--------------------------------------------------
|
|
|
|
// worker thread
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
|
|
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;
|
|
|
|
size_t 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()
|
|
|
|
{
|
|
|
|
for ( m_count = 0; m_count < 10; m_count++ )
|
|
|
|
{
|
|
|
|
// check if we were asked to exit
|
|
|
|
if ( TestDestroy() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
|
|
|
|
event.SetInt( WORKER_EVENT );
|
|
|
|
wxPostEvent( m_frame, event );
|
|
|
|
|
|
|
|
// wxSleep() can't be called from non-GUI thread!
|
|
|
|
wxThread::Sleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// main program
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
1998-06-16 13:17:02 -04:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
1998-12-26 19:54:53 -05:00
|
|
|
EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
|
1999-01-08 19:28:27 -05:00
|
|
|
EVT_MENU(TEST_CLEAR, MyFrame::OnClear)
|
1998-12-26 19:54:53 -05:00
|
|
|
EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread)
|
1999-01-22 11:21:24 -05:00
|
|
|
EVT_MENU(TEST_START_THREADS, MyFrame::OnStartThreads)
|
1998-12-26 19:54:53 -05:00
|
|
|
EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread)
|
|
|
|
EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread)
|
|
|
|
EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread)
|
1999-12-30 07:02:57 -05:00
|
|
|
|
|
|
|
EVT_MENU(TEST_START_WORKER, MyFrame::OnStartWorker)
|
|
|
|
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()
|
|
|
|
|
|
|
|
// `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
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
// Create the main frame window
|
1999-01-08 19:28:27 -05:00
|
|
|
MyFrame *frame = new MyFrame((wxFrame *)NULL, "wxWindows threads sample",
|
|
|
|
50, 50, 450, 340);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-10-19 09:58:16 -04:00
|
|
|
file_menu->Append(TEST_CLEAR, "&Clear log\tCtrl-L");
|
1999-01-08 19:28:27 -05:00
|
|
|
file_menu->AppendSeparator();
|
1998-12-26 19:54:53 -05:00
|
|
|
file_menu->Append(TEST_ABOUT, "&About");
|
1999-01-08 19:28:27 -05:00
|
|
|
file_menu->AppendSeparator();
|
1999-10-19 09:58:16 -04:00
|
|
|
file_menu->Append(TEST_QUIT, "E&xit\tAlt-X");
|
1998-12-26 19:54:53 -05:00
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
wxMenu *thread_menu = new wxMenu;
|
1999-10-19 09:58:16 -04:00
|
|
|
thread_menu->Append(TEST_START_THREAD, "&Start a new thread\tCtrl-N");
|
1999-01-22 11:21:24 -05:00
|
|
|
thread_menu->Append(TEST_START_THREADS, "Start &many threads at once");
|
1999-10-19 09:58:16 -04:00
|
|
|
thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread\tCtrl-S");
|
1998-12-26 19:54:53 -05:00
|
|
|
thread_menu->AppendSeparator();
|
1999-10-19 09:58:16 -04:00
|
|
|
thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread\tCtrl-P");
|
|
|
|
thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread\tCtrl-R");
|
1999-12-30 07:02:57 -05:00
|
|
|
thread_menu->AppendSeparator();
|
|
|
|
thread_menu->Append(TEST_START_WORKER, "Start &worker thread\tCtrl-W");
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
menu_bar->Append(thread_menu, "&Thread");
|
1998-12-26 19:54:53 -05:00
|
|
|
frame->SetMenuBar(menu_bar);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// Show the frame
|
|
|
|
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
|
|
|
|
1998-12-26 19:54:53 -05: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)
|
1998-12-26 19:54:53 -05:00
|
|
|
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
1999-01-22 11:21:24 -05:00
|
|
|
m_nRunning = m_nCount = 0;
|
|
|
|
|
|
|
|
CreateStatusBar(2);
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1999-01-08 19:28:27 -05:00
|
|
|
m_txtctrl = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(0, 0),
|
|
|
|
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
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
wxLogError("Can't create thread!");
|
|
|
|
}
|
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) )
|
|
|
|
{
|
1999-11-27 17:57:06 -05:00
|
|
|
static long s_num = 10;
|
|
|
|
|
|
|
|
s_num = wxGetNumberFromUser("How many threads to start: ", "",
|
|
|
|
"wxThread sample", s_num, 1, 10000, this);
|
|
|
|
if ( s_num == -1 )
|
|
|
|
{
|
|
|
|
s_num = 10;
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
return;
|
1999-11-27 17:57:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t count = (size_t)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
|
|
|
}
|
|
|
|
|
|
|
|
wxString msg;
|
|
|
|
msg.Printf("%d new threads created.", count);
|
|
|
|
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 17:39:58 -05:00
|
|
|
if ( thread->Run() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
|
|
|
wxLogError("Can't start thread!");
|
|
|
|
}
|
1999-01-22 11:21:24 -05:00
|
|
|
|
|
|
|
SetStatusText("New thread started.", 1);
|
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
|
|
|
{
|
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
|
|
|
{
|
|
|
|
wxLogError("No thread to stop!");
|
|
|
|
}
|
1999-01-17 17:39:58 -05:00
|
|
|
else
|
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxGetApp().m_critsect.Enter();
|
1999-01-22 11:21:24 -05:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
SetStatusText("Thread stopped.", 1);
|
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
|
|
|
{
|
1999-01-08 19:28:27 -05:00
|
|
|
wxLogError("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
|
|
|
|
|
|
|
SetStatusText("Thread resumed.", 1);
|
|
|
|
}
|
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
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
wxLogError("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
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
SetStatusText("Thread paused.", 1);
|
|
|
|
}
|
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
|
|
|
|
void MyFrame::OnIdle(wxIdleEvent &event)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
wxLogStatus(this, "%u threads total, %u running.", nCount, nRunning);
|
|
|
|
}
|
|
|
|
//else: avoid flicker - don't print anything
|
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
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
size_t count = wxGetApp().m_threads.Count();
|
1999-01-17 17:39:58 -05:00
|
|
|
for ( size_t i = 0; i < count; i++ )
|
|
|
|
{
|
1999-08-26 14:04:46 -04:00
|
|
|
wxGetApp().m_threads[0]->Delete();
|
1999-01-17 17:39:58 -05:00
|
|
|
}
|
1999-01-02 17:55:03 -05:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
Close(TRUE);
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
1998-07-27 16:50:48 -04:00
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
|
1998-06-16 13:17:02 -04:00
|
|
|
{
|
1999-01-17 17:39:58 -05:00
|
|
|
wxMessageDialog dialog(this, "wxWindows multithreaded application sample\n"
|
|
|
|
"(c) 1998 Julian Smart, Guilhem Lavaux\n"
|
|
|
|
"(c) 1999 Vadim Zeitlin",
|
1999-01-02 17:55:03 -05:00
|
|
|
"About wxThread sample",
|
|
|
|
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
|
|
|
|
|
|
|
void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
MyWorkerThread *thread = new MyWorkerThread(this);
|
|
|
|
|
|
|
|
if ( thread->Create() != wxTHREAD_NO_ERROR )
|
|
|
|
{
|
|
|
|
wxLogError("Can't create thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnWorkerEvent(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
WriteText( "Got message from worker thread\n" );
|
|
|
|
}
|
|
|
|
|