]>
git.saurik.com Git - wxWidgets.git/blob - samples/thread/test.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows thread sample
4 // Author: Julian Smart(minimal)/Guilhem Lavaux(thread test)
8 // Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "test.cpp"
14 #pragma interface "test.cpp"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #error "This sample requires thread support!"
30 #endif // wxUSE_THREADS
32 #include "wx/thread.h"
33 #include "wx/dynarray.h"
36 // Define a new application type
37 class MyApp
: public wxApp
43 WX_DEFINE_ARRAY(wxThread
*, wxArrayThread
);
45 // Define a new frame type
46 class MyFrame
: public wxFrame
50 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
53 void WriteText(const wxString
& text
) { m_txtctrl
->WriteText(text
); }
56 void OnQuit(wxCommandEvent
& event
);
57 void OnAbout(wxCommandEvent
& event
);
59 void OnStartThread(wxCommandEvent
& event
);
60 void OnStopThread(wxCommandEvent
& event
);
61 void OnPauseThread(wxCommandEvent
& event
);
62 void OnResumeThread(wxCommandEvent
& event
);
64 void OnSize(wxSizeEvent
&event
);
65 void OnIdle(wxIdleEvent
&event
);
66 bool OnClose() { return TRUE
; }
69 wxArrayThread m_threads
;
72 wxTextCtrl
*m_txtctrl
;
77 class MyThread
: public wxThread
80 MyThread(MyFrame
*frame
);
82 // thread execution starts here
83 virtual void *Entry();
85 // write something to the text control
86 void WriteText(const wxString
& text
);
93 MyThread::MyThread(MyFrame
*frame
)
100 void MyThread::WriteText(const wxString
& text
)
103 msg
<< wxTime().FormatTime() << ": " << text
;
105 // before doing any GUI calls we must ensure that this thread is the only
108 m_frame
->WriteText(msg
);
112 void *MyThread::Entry()
118 text
.Printf("Thread 0x%x started.\n", GetID());
121 for ( m_count
= 0; m_count
< 20; m_count
++ )
123 // check if we were asked to exit
127 text
.Printf("[%u] Thread 0x%x here.\n", m_count
, GetID());
133 text
.Printf("Thread 0x%x finished.\n", GetID());
139 // ID for the menu commands
145 TEST_START_THREAD
= 103,
146 TEST_STOP_THREAD
= 104,
147 TEST_PAUSE_THREAD
= 105,
148 TEST_RESUME_THREAD
= 106
151 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
152 EVT_MENU(TEST_QUIT
, MyFrame::OnQuit
)
153 EVT_MENU(TEST_ABOUT
, MyFrame::OnAbout
)
154 EVT_MENU(TEST_START_THREAD
, MyFrame::OnStartThread
)
155 EVT_MENU(TEST_STOP_THREAD
, MyFrame::OnStopThread
)
156 EVT_MENU(TEST_PAUSE_THREAD
, MyFrame::OnPauseThread
)
157 EVT_MENU(TEST_RESUME_THREAD
, MyFrame::OnResumeThread
)
159 EVT_SIZE(MyFrame::OnSize
)
161 EVT_IDLE(MyFrame::OnIdle
)
164 // Create a new application object
165 IMPLEMENT_APP (MyApp
)
167 // `Main program' equivalent, creating windows and returning main app frame
170 // Create the main frame window
171 MyFrame
*frame
= new MyFrame((wxFrame
*)NULL
, "", 50, 50, 450, 340);
174 wxMenu
*file_menu
= new wxMenu
;
176 file_menu
->Append(TEST_ABOUT
, "&About");
177 file_menu
->Append(TEST_QUIT
, "E&xit");
178 wxMenuBar
*menu_bar
= new wxMenuBar
;
179 menu_bar
->Append(file_menu
, "&File");
181 wxMenu
*thread_menu
= new wxMenu
;
182 thread_menu
->Append(TEST_START_THREAD
, "&Start a new thread");
183 thread_menu
->Append(TEST_STOP_THREAD
, "S&top a running thread");
184 thread_menu
->AppendSeparator();
185 thread_menu
->Append(TEST_PAUSE_THREAD
, "&Pause a running thread");
186 thread_menu
->Append(TEST_RESUME_THREAD
, "&Resume suspended thread");
187 menu_bar
->Append(thread_menu
, "&Thread");
188 frame
->SetMenuBar(menu_bar
);
198 // My frame constructor
199 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
200 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
202 wxPanel
*panel
= new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200),
205 m_txtctrl
= new wxTextCtrl(panel
, -1, "", wxPoint(10,30), wxSize(390, 190),
208 (void)new wxStaticText(panel
, -1, "Log window", wxPoint(10,10));
211 void MyFrame::OnStartThread(wxCommandEvent
& WXUNUSED(event
) )
213 MyThread
*thread
= new MyThread(this);
217 m_threads
.Add(thread
);
220 void MyFrame::OnStopThread(wxCommandEvent
& WXUNUSED(event
) )
222 int no_thrd
= m_threads
.Count()-1;
227 delete m_threads
[no_thrd
];
228 m_threads
.Remove(no_thrd
);
231 void MyFrame::OnResumeThread(wxCommandEvent
& WXUNUSED(event
) )
233 // resume first suspended thread
235 while ( n
< m_threads
.Count() && m_threads
[n
]->IsPaused() )
239 wxLogError("No thread to pause!");
241 m_threads
[n
]->Resume();
244 void MyFrame::OnPauseThread(wxCommandEvent
& WXUNUSED(event
) )
246 // pause last running thread
247 int n
= m_threads
.Count() - 1;
248 while ( n
>= 0 && !m_threads
[n
]->IsRunning() )
252 wxLogError("No thread to pause!");
254 m_threads
[n
]->Pause();
257 // set the frame title indicating the current number of threads
258 void MyFrame::OnIdle(wxIdleEvent
&event
)
261 nCount
= m_threads
.Count();
262 for ( size_t n
= 0; n
< nCount
; n
++ )
264 if ( m_threads
[n
]->IsRunning() )
269 title
.Printf("wxWindows thread sample (%u threads, %u running).",
274 void MyFrame::OnSize(wxSizeEvent
& event
)
276 wxFrame::OnSize(event
);
278 wxSize
size( GetClientSize() );
280 m_txtctrl
->SetSize( 10, 30, size
.x
-20, size
.y
-40 );
283 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
285 for ( size_t i
= 0; i
< m_threads
.Count(); i
++ )
291 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
293 wxMessageDialog
dialog(this, "wxThread sample (based on minimal)\n"
294 "Julian Smart and Guilhem Lavaux",
295 "About wxThread sample",
296 wxOK
| wxICON_INFORMATION
);