WaitForMultipleObjects (coredll)
Last changed: -203.59.162.121

.
Summary
This function returns a value when either any one of the specified objects is in the signaled state, or the time-out interval elapses.

C# Signature:

[DllImport("coredll.dll", EntryPoint = "WaitForMultipleObjects", SetLastError = true)]
static extern int WaitForMultipleObjects(UInt32 nCount, IntPtr[] lpHandles, Boolean fWaitAll, UInt32 dwMilliseconds);

VB Signature:

Declare Function WaitForMultipleObjects Lib "coredll.dll" (ByVal nCount As Integer, ByVal lpHandles() As IntPtr, ByVal fWaitAll As Boolean, ByVal dwMilliseconds As Integer) As Integer

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Parameters:

nCount: Specifies the number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.

lpHandles: Pointer to an array of object handles. For a list of the object types whose handles can be specified, see the Remarks section. The array can contain handles of objects of different types.

fWaitAll: Specifies the wait type. This parameter must be set to FALSE. This causes function to return when the state of any one of the objects set to is signaled. The return value indicates the object whose state caused the function to return.

dwMilliseconds: 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 Value:

If the function succeeds, the return value indicates the event that caused the function to return. The following table shows possible values.

WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount –1): the return value minus WAIT_OBJECT_0 indicates the lpHandles array index of the object that satisfied the wait. If more than one object became signaled during the call, this is the array index of the signaled object with the smallest index value of all the signaled objects.

WAIT_TIMEOUT : the time-out interval elapsed and the condition specified by the fWaitAll parameter is not satisfied.

WAIT_FAILED indicates failure. To get extended error information, call GetLastError.

Tips & Tricks:

The Handle property of .NET EventWaitHandle objects are pseudo-handles, and cannot be used with this P/Invoke function. You can only use handles obtained from the CreateEvent P/Invoke function.

Sample Code:

IntPtr hNamedEvent1 = CreateEvent(IntPtr.Zero, false, false, "namedEvent1");
IntPtr hNamedEvent2 = CreateEvent(IntPtr.Zero, false, false, "namedEvent2");
IntPtr[] handles = new IntPtr[] { hNamedEvent1, hNamedEvent2 };

while (true)
{
    Int32 result = WaitForMultipleObjects((UInt32)handles.Length, handles, false, INFINITE);
    MessageBox.Show("Event " + result + " Occured");
}

Documentation