Win32 often needs thread synchronization when writing multi-threads, many methods of synchronization, different efficiency, here you introduce an Event synchronization object. Create a MFC-based Dialog-based project, the interface is shown in Figure: // Thread part is all global variables and functions const Int max_thread = 9; handle hEvent = null; // handle to event objecthandle hthread [max_thread];
DWORD WINAPI ThreadProc (LPVOID LPPARETER) {Char BUF [64]; hwnd hlist = :: getdlgitem (THEAPP.M_PMAINWND-> M_HWND, IDC_LISTRESULT); for (;;) {sprintf (buf, "thread #% d wait.", lpParameter); ListBox_SetTopIndex (hList, ListBox_AddString (hList, buf)); WaitForSingleObject (hEvent, INFINITE); sprintf (buf, "Thread #% d work.", lpParameter); ListBox_SetTopIndex (hList, ListBox_AddString (hList, buf)); Sleep (0);
}
Void StartThread (void) {for (int i = 0; i Void killthread (void) {for (int i = 0; i Void CEVENTDLG :: Onbclean () {// Todo: add your control notification handler code here listbox_resetcontent (:: getdlgitem (this-> m_hwnd, idc_listresult);} Void CEVENTDLG :: Onbpulse () {// Todo: Add Your Control Notification Handler Code Here PulseEvent (HEVENT); Void CEVENTDLG :: OnBreset () {// Todo: Add Your Control Notification Handler Code Here ResetEvent (HEVENT) Void CEVENTDLG :: Onbsetevent () {// Todo: Add Your Control Notification Handler Code Here SetEvent (HEVENT) void CEventDlg :: OnRauto () {// TODO: Add your control notification handler code here KillThread (); if (hEvent = NULL!) {CloseHandle (hEvent);} hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); StartThread ();} void CEventDlg :: OnRmanual () {// TODO: Add your control notification handler code here KillThread (); if (! hEvent = NULL) {CloseHandle (hEvent);} hEvent = CreateEvent (NULL, TRUE, False, NULL); STARTTHREAD ();} The code is used, for example, a macro similar to ListBox_resetContent, which needs to reference the Windowsx.h header file. If you don't use these macros, you can call the sendMessage function directly.