]> git.saurik.com Git - wxWidgets.git/blob - samples/thread/test.cpp
1. Pause()/Resume() implemented for wxMSW
[wxWidgets.git] / samples / thread / test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: test.cpp
3 // Purpose: wxWindows thread sample
4 // Author: Julian Smart(minimal)/Guilhem Lavaux(thread test)
5 // Modified by:
6 // Created: 06/16/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "test.cpp"
14 #pragma interface "test.cpp"
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/thread.h"
29 #include "wx/dynarray.h"
30
31 // Define a new application type
32 class MyApp: public wxApp
33 {
34 public:
35 bool OnInit(void);
36 };
37
38 WX_DEFINE_ARRAY(wxThread *, wxArrayThread);
39
40 // Define a new frame type
41 class MyFrame: public wxFrame
42 {
43 public:
44 // ctor
45 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
46
47 // operations
48 void WriteText(const wxString& text) { m_txtctrl->WriteText(text); }
49
50 // callbacks
51 void OnQuit(wxCommandEvent& event);
52 void OnAbout(wxCommandEvent& event);
53
54 void OnStartThread(wxCommandEvent& event);
55 void OnStopThread(wxCommandEvent& event);
56 void OnPauseThread(wxCommandEvent& event);
57 void OnResumeThread(wxCommandEvent& event);
58
59 void OnSize(wxSizeEvent &event);
60 bool OnClose(void) { return TRUE; }
61
62 public:
63 wxArrayThread m_threads;
64
65 private:
66 wxTextCtrl *m_txtctrl;
67
68 DECLARE_EVENT_TABLE()
69 };
70
71 class MyThread: public wxThread
72 {
73 public:
74 MyThread(MyFrame *frame);
75
76 void *Entry();
77
78 public:
79 size_t m_count;
80 MyFrame *m_frame;
81 };
82
83 MyThread::MyThread(MyFrame *frame)
84 : wxThread()
85 {
86 m_count = 0;
87 m_frame = frame;
88 }
89
90 void *MyThread::Entry()
91 {
92 wxString text;
93
94 DeferDestroy(TRUE);
95
96 while (1) {
97 TestDestroy();
98 wxMutexGuiEnter();
99
100 text.Printf("[%u] Thread 0x%x here.\n", ++m_count, GetID());
101 m_frame->WriteText(text);
102
103 wxMutexGuiLeave();
104 wxSleep(1);
105 }
106
107 return NULL;
108 }
109
110 // ID for the menu commands
111 #define TEST_QUIT 1
112 #define TEST_TEXT 101
113 #define TEST_ABOUT 102
114 #define TEST_START_THREAD 103
115 #define TEST_STOP_THREAD 104
116 #define TEST_PAUSE_THREAD 105
117 #define TEST_RESUME_THREAD 106
118
119 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
120 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
121 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
122 EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread)
123 EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread)
124 EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread)
125 EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread)
126
127 EVT_SIZE(MyFrame::OnSize)
128 END_EVENT_TABLE()
129
130 // Create a new application object
131 IMPLEMENT_APP (MyApp)
132
133 // `Main program' equivalent, creating windows and returning main app frame
134 bool MyApp::OnInit(void)
135 {
136 // Create the main frame window
137 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows thread sample",
138 50, 50, 450, 340);
139
140 // Make a menubar
141 wxMenu *file_menu = new wxMenu;
142
143 file_menu->Append(TEST_ABOUT, "&About");
144 file_menu->Append(TEST_QUIT, "E&xit");
145 wxMenuBar *menu_bar = new wxMenuBar;
146 menu_bar->Append(file_menu, "&File");
147
148 wxMenu *thread_menu = new wxMenu;
149 thread_menu->Append(TEST_START_THREAD, "Start a new thread");
150 thread_menu->Append(TEST_STOP_THREAD, "Stop a running thread");
151 thread_menu->AppendSeparator();
152 thread_menu->Append(TEST_PAUSE_THREAD, "Pause a running thread");
153 thread_menu->Append(TEST_RESUME_THREAD, "Resume suspended thread");
154 menu_bar->Append(thread_menu, "Thread");
155 frame->SetMenuBar(menu_bar);
156
157 // Show the frame
158 frame->Show(TRUE);
159
160 SetTopWindow(frame);
161
162 return TRUE;
163 }
164
165 // My frame constructor
166 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
167 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
168 {
169 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200),
170 wxTAB_TRAVERSAL );
171
172 m_txtctrl = new wxTextCtrl(panel, -1, "", wxPoint(10,30), wxSize(390, 190),
173 wxTE_MULTILINE);
174
175 (void)new wxStaticText(panel, -1, "Log window", wxPoint(10,10));
176 }
177
178 void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
179 {
180 MyThread *thread = new MyThread(this);
181
182 thread->Create();
183
184 m_threads.Add(thread);
185 }
186
187 void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
188 {
189 int no_thrd = m_threads.Count()-1;
190
191 if (no_thrd < 0)
192 return;
193
194 delete (m_threads[no_thrd]);
195 m_threads.Remove(no_thrd);
196 }
197
198 void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
199 {
200 // resume first suspended thread
201 size_t n = 0;
202 while ( n < m_threads.Count() && m_threads[n]->IsPaused() )
203 n--;
204
205 if ( n < 0 )
206 wxLogError("No thread to pause!");
207 else
208 m_threads[n]->Resume();
209 }
210
211 void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
212 {
213 // pause last running thread
214 int n = m_threads.Count() - 1;
215 while ( n >= 0 && !m_threads[n]->IsRunning() )
216 n--;
217
218 if ( n < 0 )
219 wxLogError("No thread to pause!");
220 else
221 m_threads[n]->Pause();
222 }
223
224 void MyFrame::OnSize(wxSizeEvent& event )
225 {
226 wxFrame::OnSize(event);
227
228 wxSize size( GetClientSize() );
229
230 m_txtctrl->SetSize( 10, 30, size.x-20, size.y-40 );
231 }
232
233 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
234 {
235 unsigned int i;
236 for (i=0;i<m_threads.Count();i++)
237 delete (m_threads[i]);
238 Close(TRUE);
239 }
240
241 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
242 {
243 wxMessageDialog dialog(this, "wxThread sample (based on minimal)\nJulian Smart and Guilhem Lavaux",
244 "About wxThread sample", wxYES_NO|wxCANCEL);
245
246 dialog.ShowModal();
247 }
248
249