How to suspend the main thread until the end of the second thread?
From http://www.codeproject.com/threads/waitthreadmsi.asp#xx441032xx
Ybbozman
Don't be frightened by thread, let's take a look at it;)
As a translation, I encountered a similar problem with YBbozman in VC . In the primary application (main process - actually still thread, the main thread can also be), when you click the "Find" button, I made me. The event of a search recursive search file - this event will be very slow, resulting in the significant interfacial redrawing, the CPU resource usage is very high, the reason is that the Windows preemptive multitasking will be the highest level of this main thread. Find file events are events as this primary thread being executed, and most of the CPUs will be given to find file events, while the operating rate of interface redrawing will become unpredictable.
Start a new working thread for this Find file event, is very efficient, looking for file events in new work threads, but the priority is not higher than the current main thread, which guarantees the interface redraw operation Normal, the problem is that I want to continue to execute some of the main threads after the finding file event is completed, which involves the problem between thread waiting - Wait (called synchronization -synchronization better), Ybbozman's question and I have something like this, and I look at how YBozman is how to wait for the thread.
Introduction
A few weeks ago, I need to make a dialog box as part of the MSI installer (Windows2000 software installation), and the code written to this must be released as a regular (rule) DLL. Whether this dialog is visible, depending on the result of the installation completion, that is, the function depends on another thread (the working thread is being performed here) is completed, and the thread is terminated after the installation is complete, and this conversation is finally displayed. frame.
solve
You must write two functions, the first void cmytestDialog :: PeekMessageloop () Get a message from the message queue:
Void CMYTESTDIALOG :: peekMessageloop ()
{
MSG msg;
While (PeekMessage (& MSG, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
}
The second, void cmytestdialog :: waitforthreadtoterminate (Handle Hthread) will indicate which thread needs to wait and further process:
Void CMYTESTDIALOG :: WaitForthReadtoterminate (Handle Hthread)
{
DWORD DWRET;
DO
{
DWRET = :: MsgwaitFormultipleObjects (1, & Hthread, False,
Infinite, QS_allInput);
IF (dwret! = WAIT_Object_0)
{
PeekMessageloop ();
}
} while ((dwret! = wait_object_0) && (dwret! = wait_failed);
}
Sample code
Suppose there is a button on the dialog, when the button is clicked, start starting the second thread, wait until the second thread is complete, we will continue the main thread down:
Void CMYTESTDIALOG :: Onbutton1 ()
{
m_pupdatethread = afxbeginthread (UpdatedEviceContent,
(LPVOID) this / *, thread_priority_below_normal * /);
IF (m_pupdatethread)
{
WaitForthReadtoterminate (m_pupdatethread-> m_hthread);
}
// do wherever you want after the action is finished
}
YBozman uses the API function of synchronization between the thread when resolving this problem :: msgwaitformultipleObjects function, it is a signal state for waiting for an object (here is a thread T2 to wait) - Semaphore (Dutch computer scientist Dijkstra) The synchronization problem of the process or thread is an abstraction of the signal state, which is a synchronization function between a thread of a thread that is paid in the Netherlands on August 6th, 2002. When arriving (using a specific message -Message indicator), the last parameter qs_allinput of :: msgwaitformultipleObjects will return to the last parameter qs_allinput of :: MsGwaitFormultiPut Specify that all input messages can be reached as a signal state and therefore returned, peekMessageloop () will be a message queue All message signs are old messages, so :: msgwaitformultipleObjects does not return, and finally, only when the thread T2 is completed, returns WAIT_Object_0.
About waiting for multiple thread synchronization, reference :: MsgwaitFormultiPleObjects function usage.
The wrong mistake is inevitable, please know the people of insight