WaitForSingleObject的用法
DWORD WaitForSingleObject(
HANDLE hHandle,
DWORD dwMilliseconds
);
參數hHandle是一個事件的句柄,第二個參數dwMilliseconds是時間間隔。如果時間是有信号狀态傳回WAIT_OBJECT_0,如果時間超過dwMilliseconds值但時間事件還是無信号狀态則傳回WAIT_TIMEOUT。
hHandle可以是下列對象的句柄:
Change notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable timer
WaitForSingleObject函數用來檢測hHandle事件的信号狀态,當函數的執行時間超過dwMilliseconds就傳回,但如果參數dwMilliseconds為INFINITE時函數将直到相應時間事件變成有信号狀态才傳回,否則就一直等待下去,直到WaitForSingleObject有傳回直才執行後面的代碼。在這裡舉個例子:
先建立一個全局Event對象g_event:
CEvent g_event;
在程式中可以通過調用CEvent::SetEvent設定事件為有信号狀态。
下面是一個線程函數MyThreadPro()
UINT CFlushDlg::MyThreadProc( LPVOID pParam )
{
WaitForSingleObject(g_event,INFINITE);
For(;;)
{
………….
}
return 0;
}
在這個線程函數中隻有設定g_event為有信号狀态時才執行下面的for循環,因為g_event是全局變量,是以我們可以在别的線程中通過g_event. SetEvent控制這個線程。
還有一種用法就是我們可以通過WaitForSingleObject函數來間隔的執行一個線程函數的函數體
UINT CFlushDlg::MyThreadProc( LPVOID pParam )
{
while(WaitForSingleObject(g_event,MT_INTERVAL)!=WAIT_OBJECT_0)
{
………………
}
return 0;
}
在這個線程函數中可以可以通過設定MT_INTERVAL來控制這個線程的函數體多久執行一次,當事件為無信号狀态是函數體隔MT_INTERVAL執行一次,當設定事件為有信号狀态時,線程就執行完畢了。
WaitForMultipleObjects
The WaitForMultipleObjects function returns when one of the following occurs:
Either any one or all of the specified objects are in the signaled state.
The time-out interval elapses.
To enter an alertable wait state, use the WaitForMultipleObjectsEx function.
DWORD WaitForMultipleObjects(
DWORD nCount, // number of handles in array
CONST HANDLE *lpHandles, // object-handle array
BOOL bWaitAll, // wait option
DWORD dwMilliseconds // time-out interval
);
Parameters
nCount
[in] Specifies the number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.
lpHandles
[in] Pointer to an array of object handles. For a list of the object types whose handles can be specified, see the following Remarks section. The array can contain handles to objects of different types. It may not contain the multiple copies of the same handle.
If one of these handles is closed while the wait is still pending, the function's behavior is undefined.
Windows NT/2000/XP: The handles must have SYNCHRONIZE access. For more information, see Standard Access Rights.
Windows 95/98/Me: No handle may be a duplicate of another handle created using DuplicateHandle.
bWaitAll
[in] Specifies the wait type. If TRUE, the function returns when the state of all objects in the lpHandles array is signaled. If FALSE, the function returns when the state of any one of the objects is set to signaled. In the latter case, the return value indicates the object whose state caused the function to return.
dwMilliseconds
[in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the conditions specified by the bWaitAll parameter are not met. If dwMilliseconds is zero, the function tests the states of the specified objects and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
Return Values
If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following.