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
|
|
|
/*
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
1. show how SetPriority() works.
|
|
|
|
2. use worker threads to update progress controls instead of writing
|
|
|
|
messages - it will be more visual
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
public:
|
1999-01-17 17:39:58 -05:00
|
|
|
bool OnInit();
|
1998-06-16 13:17:02 -04:00
|
|
|
};
|
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
class MyThread;
|
1998-12-26 19:54:53 -05:00
|
|
|
WX_DEFINE_ARRAY(wxThread *, wxArrayThread);
|
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-01-02 17:55:03 -05:00
|
|
|
void OnIdle(wxIdleEvent &event);
|
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
// called by dying thread _in_that_thread_context_
|
1999-01-08 19:28:27 -05:00
|
|
|
void OnThreadExit(wxThread *thread);
|
|
|
|
|
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-22 11:21:24 -05:00
|
|
|
// crit section protects access to all of the arrays below
|
1999-01-08 19:28:27 -05:00
|
|
|
wxCriticalSection m_critsect;
|
|
|
|
|
1999-01-22 11:21:24 -05:00
|
|
|
// all the threads currently alive - as soon as the thread terminates, it's
|
|
|
|
// removed from the array
|
|
|
|
wxArrayThread m_threads;
|
|
|
|
|
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-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-03-12 11:09:09 -05:00
|
|
|
msg << wxTime().FormatTime() << ": " << text;
|
1999-01-08 19:28:27 -05:00
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
m_frame->WriteText(msg);
|
1999-05-22 04:01:17 -04:00
|
|
|
|
|
|
|
wxMutexGuiLeave();
|
1999-01-08 19:28:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyThread::OnExit()
|
|
|
|
{
|
|
|
|
m_frame->OnThreadExit(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
|
|
|
|
|
|
|
text.Printf("Thread 0x%x started.\n", GetID());
|
|
|
|
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;
|
|
|
|
|
|
|
|
text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID());
|
|
|
|
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
|
|
|
|
|
|
|
text.Printf("Thread 0x%x finished.\n", GetID());
|
|
|
|
WriteText(text);
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
return NULL;
|
1998-06-16 13:17:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID for the menu commands
|
1999-01-02 17:55:03 -05:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TEST_QUIT = 1,
|
|
|
|
TEST_TEXT = 101,
|
1999-01-22 11:21:24 -05:00
|
|
|
TEST_ABOUT,
|
|
|
|
TEST_CLEAR,
|
|
|
|
TEST_START_THREAD = 201,
|
|
|
|
TEST_START_THREADS,
|
|
|
|
TEST_STOP_THREAD,
|
|
|
|
TEST_PAUSE_THREAD,
|
|
|
|
TEST_RESUME_THREAD
|
1999-01-02 17:55:03 -05:00
|
|
|
};
|
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-01-02 17:55:03 -05:00
|
|
|
EVT_IDLE(MyFrame::OnIdle)
|
1998-06-16 13:17:02 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// Create a new application object
|
1999-01-02 17:55:03 -05:00
|
|
|
IMPLEMENT_APP (MyApp)
|
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
|
|
|
{
|
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-01-08 19:28:27 -05:00
|
|
|
file_menu->Append(TEST_CLEAR, "&Clear log");
|
|
|
|
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();
|
1998-12-26 19:54:53 -05:00
|
|
|
file_menu->Append(TEST_QUIT, "E&xit");
|
|
|
|
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-01-02 17:55:03 -05:00
|
|
|
thread_menu->Append(TEST_START_THREAD, "&Start a new thread");
|
1999-01-22 11:21:24 -05:00
|
|
|
thread_menu->Append(TEST_START_THREADS, "Start &many threads at once");
|
1999-01-02 17:55:03 -05:00
|
|
|
thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread");
|
1998-12-26 19:54:53 -05:00
|
|
|
thread_menu->AppendSeparator();
|
1999-01-02 17:55:03 -05:00
|
|
|
thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread");
|
|
|
|
thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread");
|
|
|
|
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-01-08 19:28:27 -05:00
|
|
|
wxCriticalSectionLocker enter(m_critsect);
|
1998-12-26 19:54:53 -05:00
|
|
|
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) )
|
|
|
|
{
|
|
|
|
static wxString s_str;
|
|
|
|
s_str = wxGetTextFromUser("How many threads to start: ",
|
|
|
|
"wxThread sample",
|
|
|
|
s_str, this);
|
|
|
|
if ( s_str.IsEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t count, n;
|
|
|
|
sscanf(s_str, "%u", &count);
|
|
|
|
if ( count == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wxArrayThread threads;
|
|
|
|
|
|
|
|
// first create them all...
|
|
|
|
for ( n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
threads.Add(CreateThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if ( 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-01-22 11:21:24 -05:00
|
|
|
m_critsect.Enter();
|
|
|
|
|
|
|
|
wxThread *thread = m_threads.Last();
|
|
|
|
|
|
|
|
// it's important to leave critical section before calling Delete()
|
|
|
|
// because delete will (implicitly) call OnThreadExit() which also tries
|
|
|
|
// to enter the same crit section - would dead lock.
|
|
|
|
m_critsect.Leave();
|
|
|
|
|
|
|
|
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-01-08 19:28:27 -05:00
|
|
|
wxCriticalSectionLocker enter(m_critsect);
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
// resume first suspended thread
|
1999-01-17 17:39:58 -05:00
|
|
|
size_t n = 0, count = m_threads.Count();
|
|
|
|
while ( n < count && !m_threads[n]->IsPaused() )
|
|
|
|
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
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
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-01-08 19:28:27 -05:00
|
|
|
wxCriticalSectionLocker enter(m_critsect);
|
|
|
|
|
1999-01-02 17:55:03 -05:00
|
|
|
// pause last running thread
|
1998-12-26 19:54:53 -05:00
|
|
|
int n = m_threads.Count() - 1;
|
|
|
|
while ( n >= 0 && !m_threads[n]->IsRunning() )
|
|
|
|
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
|
|
|
{
|
1998-12-26 19:54:53 -05:00
|
|
|
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,
|
|
|
|
nCount = m_threads.Count();
|
|
|
|
for ( size_t n = 0; n < nCount; n++ )
|
|
|
|
{
|
|
|
|
if ( m_threads[n]->IsRunning() )
|
|
|
|
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-01-17 17:39:58 -05:00
|
|
|
size_t count = m_threads.Count();
|
1999-08-26 13:35:00 -04:00
|
|
|
m_critsect.Enter();
|
1999-01-17 17:39:58 -05:00
|
|
|
for ( size_t i = 0; i < count; i++ )
|
|
|
|
{
|
1999-08-26 13:35:00 -04:00
|
|
|
wxThread *thread = m_threads[0];
|
|
|
|
m_threads.Remove(thread);
|
1999-08-01 03:26:36 -04:00
|
|
|
// We must always use 0 because Delete() calls OnThreadExit() and
|
1999-08-26 13:35:00 -04:00
|
|
|
// OnThreadExit() removes 0 from the array.
|
|
|
|
thread->Delete();
|
1999-01-17 17:39:58 -05:00
|
|
|
}
|
1999-08-26 13:35:00 -04:00
|
|
|
m_critsect.Leave();
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnThreadExit(wxThread *thread)
|
|
|
|
{
|
|
|
|
}
|