/////////////////////////////////////////////////////////////////////////////
/*
- TODO:
-
- 1. show how SetPriority() works.
- 2. use worker threads to update progress controls instead of writing
- messages - it will be more visual
+ TODO: use worker threads to update progress controls instead of writing
+ messages - it will be more visual
*/
#ifdef __GNUG__
// Define a new application type
class MyApp : public wxApp
{
- 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;
- public:
- bool OnInit();
+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;
};
// Create a new application object
-IMPLEMENT_APP (MyApp)
+IMPLEMENT_APP(MyApp)
// Define a new frame type
class MyFrame: public wxFrame
// helper function - creates a new thread (but doesn't run it)
MyThread *CreateThread();
-
// just some place to put our messages in
wxTextCtrl *m_txtctrl;
wxMutexGuiEnter();
- msg << wxTime().FormatTime() << ": " << text;
+ msg << text;
m_frame->WriteText(msg);
{
wxString text;
- text.Printf("Thread 0x%x started.\n", GetID());
+ text.Printf("Thread 0x%x started (priority = %d).\n",
+ GetId(), GetPriority());
WriteText(text);
for ( m_count = 0; m_count < 10; m_count++ )
if ( TestDestroy() )
break;
- text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID());
+ text.Printf("[%u] Thread 0x%x here.\n", m_count, GetId());
WriteText(text);
// wxSleep() can't be called from non-GUI thread!
wxThread::Sleep(1000);
}
- text.Printf("Thread 0x%x finished.\n", GetID());
+ text.Printf("Thread 0x%x finished.\n", GetId());
WriteText(text);
return NULL;
// Make a menubar
wxMenu *file_menu = new wxMenu;
- file_menu->Append(TEST_CLEAR, "&Clear log");
+ file_menu->Append(TEST_CLEAR, "&Clear log\tCtrl-L");
file_menu->AppendSeparator();
file_menu->Append(TEST_ABOUT, "&About");
file_menu->AppendSeparator();
- file_menu->Append(TEST_QUIT, "E&xit");
+ file_menu->Append(TEST_QUIT, "E&xit\tAlt-X");
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "&File");
wxMenu *thread_menu = new wxMenu;
- thread_menu->Append(TEST_START_THREAD, "&Start a new thread");
+ thread_menu->Append(TEST_START_THREAD, "&Start a new thread\tCtrl-N");
thread_menu->Append(TEST_START_THREADS, "Start &many threads at once");
- thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread");
+ thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread\tCtrl-S");
thread_menu->AppendSeparator();
- thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread");
- thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread");
+ thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread\tCtrl-P");
+ thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread\tCtrl-R");
menu_bar->Append(thread_menu, "&Thread");
frame->SetMenuBar(menu_bar);
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;
+ 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;
- size_t count, n;
- sscanf(s_str, "%u", &count);
- if ( count == 0 )
return;
+ }
+
+ size_t count = (size_t)s_num, n;
wxArrayThread threads;
// first create them all...
for ( n = 0; n < count; n++ )
{
- threads.Add(CreateThread());
+ 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);
}
wxString msg;