]>
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
40 WX_DEFINE_ARRAY(wxThread
*,wxArrayThread
);
42 // Define a new frame type
43 class MyFrame
: public wxFrame
46 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
49 void OnQuit(wxCommandEvent
& event
);
50 void OnAbout(wxCommandEvent
& event
);
51 void OnStartThread(wxCommandEvent
& event
);
52 void OnStopThread(wxCommandEvent
& event
);
53 void OnPauseThread(wxCommandEvent
& event
);
54 bool OnClose(void) { return TRUE
; }
57 wxArrayThread m_threads
;
58 wxTextCtrl
*m_txtctrl
;
63 class MyThread
: public wxThread
66 MyThread(MyFrame
*frame
);
73 MyThread::MyThread(MyFrame
*frame
)
79 void *MyThread::Entry()
83 text
.Printf("Thread 0x%x: Hello !\n", GetID());
89 m_frame
->m_txtctrl
->WriteText(text
);
97 // ID for the menu commands
100 #define TEST_ABOUT 102
101 #define TEST_START_THREAD 103
102 #define TEST_STOP_THREAD 104
103 #define TEST_PAUSE_THREAD 105
105 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
106 EVT_MENU(TEST_QUIT
, MyFrame::OnQuit
)
107 EVT_MENU(TEST_ABOUT
, MyFrame::OnAbout
)
108 EVT_MENU(TEST_START_THREAD
, MyFrame::OnStartThread
)
109 EVT_MENU(TEST_STOP_THREAD
, MyFrame::OnStopThread
)
110 EVT_MENU(TEST_PAUSE_THREAD
, MyFrame::OnPauseThread
)
113 // Create a new application object
114 IMPLEMENT_APP (MyApp
)
116 // `Main program' equivalent, creating windows and returning main app frame
117 bool MyApp::OnInit(void)
119 // Create the main frame window
120 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Minimal wxWindows App", 50, 50, 450, 340);
123 wxMenu
*file_menu
= new wxMenu
;
125 file_menu
->Append(TEST_ABOUT
, "&About");
126 file_menu
->Append(TEST_QUIT
, "E&xit");
127 wxMenuBar
*menu_bar
= new wxMenuBar
;
128 menu_bar
->Append(file_menu
, "&File");
130 wxMenu
*thread_menu
= new wxMenu
;
131 thread_menu
->Append(TEST_START_THREAD
, "Start a new thread");
132 thread_menu
->Append(TEST_STOP_THREAD
, "Stop a running thread");
133 thread_menu
->Append(TEST_PAUSE_THREAD
, "Pause a running thread");
134 menu_bar
->Append(thread_menu
, "Thread");
135 frame
->SetMenuBar(menu_bar
);
137 // Make a panel with a message
138 wxPanel
*panel
= new wxPanel(frame
, -1, wxPoint(0, 0), wxSize(400, 200), wxTAB_TRAVERSAL
);
140 frame
->m_txtctrl
= new wxTextCtrl(panel
, -1, "", wxPoint(10, 10), wxSize(390, 190),
151 // My frame constructor
152 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
153 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
156 void MyFrame::OnStartThread(wxCommandEvent
& WXUNUSED(event
) )
158 MyThread
*thread
= new MyThread(this);
162 m_threads
.Add(thread
);
165 void MyFrame::OnStopThread(wxCommandEvent
& WXUNUSED(event
) )
167 int no_thrd
= m_threads
.Count()-1;
172 delete (m_threads
[no_thrd
]);
173 m_threads
.Remove(no_thrd
);
176 void MyFrame::OnPauseThread(wxCommandEvent
& WXUNUSED(event
) )
179 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
182 for (i
=0;i
<m_threads
.Count();i
++)
183 delete (m_threads
[i
]);
187 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
189 wxMessageDialog
dialog(this, "wxThread sample (based on minimal)\nJulian Smart and Guilhem Lavaux",
190 "About wxThread sample", wxYES_NO
|wxCANCEL
);