]>
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"
28 #include "wx/thread.h"
29 #include "wx/dynarray.h"
31 // Define a new application type
32 class MyApp
: public wxApp
38 WX_DEFINE_ARRAY(wxThread
*, wxArrayThread
);
40 // Define a new frame type
41 class MyFrame
: public wxFrame
45 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
48 void WriteText(const wxString
& text
) { m_txtctrl
->WriteText(text
); }
51 void OnQuit(wxCommandEvent
& event
);
52 void OnAbout(wxCommandEvent
& event
);
54 void OnStartThread(wxCommandEvent
& event
);
55 void OnStopThread(wxCommandEvent
& event
);
56 void OnPauseThread(wxCommandEvent
& event
);
57 void OnResumeThread(wxCommandEvent
& event
);
59 void OnSize(wxSizeEvent
&event
);
60 bool OnClose(void) { return TRUE
; }
63 wxArrayThread m_threads
;
66 wxTextCtrl
*m_txtctrl
;
71 class MyThread
: public wxThread
74 MyThread(MyFrame
*frame
);
83 MyThread::MyThread(MyFrame
*frame
)
90 void *MyThread::Entry()
100 text
.Printf("[%u] Thread 0x%x here.\n", ++m_count
, GetID());
101 m_frame
->WriteText(text
);
110 // ID for the menu commands
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
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
)
127 EVT_SIZE(MyFrame::OnSize
)
130 // Create a new application object
131 IMPLEMENT_APP (MyApp
)
133 // `Main program' equivalent, creating windows and returning main app frame
134 bool MyApp::OnInit(void)
136 // Create the main frame window
137 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxWindows thread sample",
141 wxMenu
*file_menu
= new wxMenu
;
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");
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
);
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
))
169 wxPanel
*panel
= new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200),
172 m_txtctrl
= new wxTextCtrl(panel
, -1, "", wxPoint(10,30), wxSize(390, 190),
175 (void)new wxStaticText(panel
, -1, "Log window", wxPoint(10,10));
178 void MyFrame::OnStartThread(wxCommandEvent
& WXUNUSED(event
) )
180 MyThread
*thread
= new MyThread(this);
184 m_threads
.Add(thread
);
187 void MyFrame::OnStopThread(wxCommandEvent
& WXUNUSED(event
) )
189 int no_thrd
= m_threads
.Count()-1;
194 delete (m_threads
[no_thrd
]);
195 m_threads
.Remove(no_thrd
);
198 void MyFrame::OnResumeThread(wxCommandEvent
& WXUNUSED(event
) )
200 // resume first suspended thread
202 while ( n
< m_threads
.Count() && m_threads
[n
]->IsPaused() )
206 wxLogError("No thread to pause!");
208 m_threads
[n
]->Resume();
211 void MyFrame::OnPauseThread(wxCommandEvent
& WXUNUSED(event
) )
213 // pause last running thread
214 int n
= m_threads
.Count() - 1;
215 while ( n
>= 0 && !m_threads
[n
]->IsRunning() )
219 wxLogError("No thread to pause!");
221 m_threads
[n
]->Pause();
224 void MyFrame::OnSize(wxSizeEvent
& event
)
226 wxFrame::OnSize(event
);
228 wxSize
size( GetClientSize() );
230 m_txtctrl
->SetSize( 10, 30, size
.x
-20, size
.y
-40 );
233 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
236 for (i
=0;i
<m_threads
.Count();i
++)
237 delete (m_threads
[i
]);
241 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
243 wxMessageDialog
dialog(this, "wxThread sample (based on minimal)\nJulian Smart and Guilhem Lavaux",
244 "About wxThread sample", wxYES_NO
|wxCANCEL
);