thread fixes for MSW (samples doesn't compile currently under !MSW, will
be fixed a.s.a.p.) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1351 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -9,9 +9,17 @@
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
TODO:
|
||||
|
||||
1. show how SetPriority() works.
|
||||
2. use worker threads to update progress controls instead of writing
|
||||
messages - it will be more visual
|
||||
*/
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "test.cpp"
|
||||
#pragma interface "test.cpp"
|
||||
#pragma implementation "test.cpp"
|
||||
#pragma interface "test.cpp"
|
||||
#endif
|
||||
|
||||
// For compilers that support precompilation, includes "wx/wx.h".
|
||||
@@ -55,26 +63,35 @@ public:
|
||||
// callbacks
|
||||
void OnQuit(wxCommandEvent& event);
|
||||
void OnAbout(wxCommandEvent& event);
|
||||
void OnClear(wxCommandEvent& event);
|
||||
|
||||
void OnStartThread(wxCommandEvent& event);
|
||||
void OnStopThread(wxCommandEvent& event);
|
||||
void OnPauseThread(wxCommandEvent& event);
|
||||
void OnResumeThread(wxCommandEvent& event);
|
||||
|
||||
void OnSize(wxSizeEvent &event);
|
||||
void OnIdle(wxIdleEvent &event);
|
||||
bool OnClose() { return TRUE; }
|
||||
|
||||
// called by dying thread
|
||||
void OnThreadExit(wxThread *thread);
|
||||
|
||||
public:
|
||||
wxArrayThread m_threads;
|
||||
|
||||
private:
|
||||
wxTextCtrl *m_txtctrl;
|
||||
void DeleteThread(size_t index);
|
||||
|
||||
// crit section protects access to the array below
|
||||
wxCriticalSection m_critsect;
|
||||
wxArrayInt m_aToDelete;
|
||||
|
||||
wxTextCtrl *m_txtctrl;
|
||||
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
||||
class MyThread: public wxThread
|
||||
class MyThread : public wxThread
|
||||
{
|
||||
public:
|
||||
MyThread(MyFrame *frame);
|
||||
@@ -82,6 +99,9 @@ public:
|
||||
// thread execution starts here
|
||||
virtual void *Entry();
|
||||
|
||||
// called when the thread exits - whether
|
||||
virtual void OnExit();
|
||||
|
||||
// write something to the text control
|
||||
void WriteText(const wxString& text);
|
||||
|
||||
@@ -104,9 +124,14 @@ void MyThread::WriteText(const wxString& text)
|
||||
|
||||
// before doing any GUI calls we must ensure that this thread is the only
|
||||
// one doing it!
|
||||
wxMutexGuiEnter();
|
||||
wxMutexGuiLocker guiLocker;
|
||||
|
||||
m_frame->WriteText(msg);
|
||||
wxMutexGuiLeave();
|
||||
}
|
||||
|
||||
void MyThread::OnExit()
|
||||
{
|
||||
m_frame->OnThreadExit(this);
|
||||
}
|
||||
|
||||
void *MyThread::Entry()
|
||||
@@ -118,7 +143,7 @@ void *MyThread::Entry()
|
||||
text.Printf("Thread 0x%x started.\n", GetID());
|
||||
WriteText(text);
|
||||
|
||||
for ( m_count = 0; m_count < 20; m_count++ )
|
||||
for ( m_count = 0; m_count < 10; m_count++ )
|
||||
{
|
||||
// check if we were asked to exit
|
||||
if ( TestDestroy() )
|
||||
@@ -127,7 +152,11 @@ void *MyThread::Entry()
|
||||
text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID());
|
||||
WriteText(text);
|
||||
|
||||
#ifdef __WXMSW__
|
||||
::Sleep(1000);
|
||||
#else
|
||||
wxSleep(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
text.Printf("Thread 0x%x finished.\n", GetID());
|
||||
@@ -142,22 +171,22 @@ enum
|
||||
TEST_QUIT = 1,
|
||||
TEST_TEXT = 101,
|
||||
TEST_ABOUT = 102,
|
||||
TEST_START_THREAD = 103,
|
||||
TEST_STOP_THREAD = 104,
|
||||
TEST_PAUSE_THREAD = 105,
|
||||
TEST_RESUME_THREAD = 106
|
||||
TEST_CLEAR = 103,
|
||||
TEST_START_THREAD = 203,
|
||||
TEST_STOP_THREAD = 204,
|
||||
TEST_PAUSE_THREAD = 205,
|
||||
TEST_RESUME_THREAD = 206
|
||||
};
|
||||
|
||||
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
||||
EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
|
||||
EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
|
||||
EVT_MENU(TEST_CLEAR, MyFrame::OnClear)
|
||||
EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread)
|
||||
EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread)
|
||||
EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread)
|
||||
EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread)
|
||||
|
||||
EVT_SIZE(MyFrame::OnSize)
|
||||
|
||||
EVT_IDLE(MyFrame::OnIdle)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
@@ -168,12 +197,16 @@ IMPLEMENT_APP (MyApp)
|
||||
bool MyApp::OnInit()
|
||||
{
|
||||
// Create the main frame window
|
||||
MyFrame *frame = new MyFrame((wxFrame *)NULL, "", 50, 50, 450, 340);
|
||||
MyFrame *frame = new MyFrame((wxFrame *)NULL, "wxWindows threads sample",
|
||||
50, 50, 450, 340);
|
||||
|
||||
// Make a menubar
|
||||
wxMenu *file_menu = new wxMenu;
|
||||
|
||||
file_menu->Append(TEST_CLEAR, "&Clear log");
|
||||
file_menu->AppendSeparator();
|
||||
file_menu->Append(TEST_ABOUT, "&About");
|
||||
file_menu->AppendSeparator();
|
||||
file_menu->Append(TEST_QUIT, "E&xit");
|
||||
wxMenuBar *menu_bar = new wxMenuBar;
|
||||
menu_bar->Append(file_menu, "&File");
|
||||
@@ -199,13 +232,11 @@ bool MyApp::OnInit()
|
||||
MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
|
||||
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
|
||||
{
|
||||
wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200),
|
||||
wxTAB_TRAVERSAL);
|
||||
CreateStatusBar();
|
||||
|
||||
m_txtctrl = new wxTextCtrl(panel, -1, "", wxPoint(10,30), wxSize(390, 190),
|
||||
wxTE_MULTILINE);
|
||||
m_txtctrl = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(0, 0),
|
||||
wxTE_MULTILINE | wxTE_READONLY);
|
||||
|
||||
(void)new wxStaticText(panel, -1, "Log window", wxPoint(10,10));
|
||||
}
|
||||
|
||||
void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
|
||||
@@ -214,35 +245,43 @@ void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
|
||||
|
||||
thread->Create();
|
||||
|
||||
wxCriticalSectionLocker enter(m_critsect);
|
||||
m_threads.Add(thread);
|
||||
}
|
||||
|
||||
void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
|
||||
{
|
||||
int no_thrd = m_threads.Count()-1;
|
||||
int no_thrd = m_threads.Count() - 1;
|
||||
|
||||
if ( no_thrd < 0 )
|
||||
{
|
||||
wxLogError("No thread to stop!");
|
||||
|
||||
if (no_thrd < 0)
|
||||
return;
|
||||
}
|
||||
|
||||
delete m_threads[no_thrd];
|
||||
m_threads.Remove(no_thrd);
|
||||
DeleteThread(no_thrd);
|
||||
}
|
||||
|
||||
void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
|
||||
{
|
||||
wxCriticalSectionLocker enter(m_critsect);
|
||||
|
||||
// resume first suspended thread
|
||||
size_t n = 0;
|
||||
while ( n < m_threads.Count() && m_threads[n]->IsPaused() )
|
||||
n--;
|
||||
|
||||
if ( n < 0 )
|
||||
wxLogError("No thread to pause!");
|
||||
wxLogError("No thread to resume!");
|
||||
else
|
||||
m_threads[n]->Resume();
|
||||
}
|
||||
|
||||
void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
|
||||
{
|
||||
wxCriticalSectionLocker enter(m_critsect);
|
||||
|
||||
// pause last running thread
|
||||
int n = m_threads.Count() - 1;
|
||||
while ( n >= 0 && !m_threads[n]->IsRunning() )
|
||||
@@ -257,6 +296,17 @@ void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
|
||||
// set the frame title indicating the current number of threads
|
||||
void MyFrame::OnIdle(wxIdleEvent &event)
|
||||
{
|
||||
// first remove from the array all the threads which died since last call
|
||||
{
|
||||
wxCriticalSectionLocker enter(m_critsect);
|
||||
|
||||
size_t nCount = m_aToDelete.Count();
|
||||
for ( size_t n = 0; n < nCount; n++ )
|
||||
DeleteThread((size_t)m_aToDelete[n]);
|
||||
|
||||
m_aToDelete.Empty();
|
||||
}
|
||||
|
||||
size_t nRunning = 0,
|
||||
nCount = m_threads.Count();
|
||||
for ( size_t n = 0; n < nCount; n++ )
|
||||
@@ -265,19 +315,7 @@ void MyFrame::OnIdle(wxIdleEvent &event)
|
||||
nRunning++;
|
||||
}
|
||||
|
||||
wxString title;
|
||||
title.Printf("wxWindows thread sample (%u threads, %u running).",
|
||||
nCount, nRunning);
|
||||
SetTitle(title);
|
||||
}
|
||||
|
||||
void MyFrame::OnSize(wxSizeEvent& event)
|
||||
{
|
||||
wxFrame::OnSize(event);
|
||||
|
||||
wxSize size( GetClientSize() );
|
||||
|
||||
m_txtctrl->SetSize( 10, 30, size.x-20, size.y-40 );
|
||||
wxLogStatus(this, "%u threads total, %u running.", nCount, nRunning);
|
||||
}
|
||||
|
||||
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
|
||||
@@ -291,11 +329,30 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
|
||||
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
|
||||
{
|
||||
wxMessageDialog dialog(this, "wxThread sample (based on minimal)\n"
|
||||
"Julian Smart and Guilhem Lavaux",
|
||||
"Julian Smart, Guilhem Lavaux, Vadim Zeitlin",
|
||||
"About wxThread sample",
|
||||
wxOK | wxICON_INFORMATION);
|
||||
|
||||
dialog.ShowModal();
|
||||
}
|
||||
|
||||
void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
m_txtctrl->Clear();
|
||||
}
|
||||
|
||||
void MyFrame::OnThreadExit(wxThread *thread)
|
||||
{
|
||||
int index = m_threads.Index(thread);
|
||||
wxCHECK_RET( index != -1, "unknown thread being deleted??" );
|
||||
|
||||
wxCriticalSectionLocker enter(m_critsect);
|
||||
|
||||
m_aToDelete.Add(index);
|
||||
}
|
||||
|
||||
void MyFrame::DeleteThread(size_t index)
|
||||
{
|
||||
delete m_threads[index];
|
||||
m_threads.Remove(index);
|
||||
}
|
Reference in New Issue
Block a user