This is a mirror of official site: http://jasper-net.blogspot.com/

Why does WaitForMultipleObjects return ERROR_INVALID_PARAMETER when all the parameters look valid to me?

| Monday, February 28, 2011
A customer asked for assistance with the WaitForMultipleObjects function:

I am getting ERROR_INVALID_PARAMETER when calling WaitForMultipleObjects even though all the parameters are valid as far as I can tell. I've narrowed it down to this simple program.

int main()
{
int i;
HANDLE Handles[4];

// Create the events
for (i = 0; i < 4; i++) {
 Handles[i] = CreateEvent(NULL, FALSE, FALSE, TEXT("Test"));
 if (Handles[i] == NULL) {
  printf("Failed to create event - test failed\n"); return 0;
 }
}

// Set them all to signaled
for (i = 0; i < 4; i++) SetEvent(Handles[i]);

// Wait for all of them - we expect this to return WAIT_OBJECT_0
printf("WaitForMultipleObjects returned %d\n",
       WaitForMultipleObjects(4, Handles, TRUE, INFINITE));

return 0;
}


First of all, thank you for narrowing the issue down to a minimal program that illustrates the problem. You'd be surprised how often a customer says, "I'm having problem with function X. Here's a program that illustrates the problem." And then attaches a huge project that doesn't compile because it is written in some development environment different from the one you have on your machine.

The problem here is that you are passing four handles to the same event to WaitForMultipleObjects with the bWaitAll parameter set to TRUE. The WaitForMultipleObjects function rejects duplicates if you ask it to wait for all of the objects. Why is that?

Well, consider this program: It creates a named auto-reset event (as is "obvious" from the FALSE second parameter passed to CreateEvent) and stores a handle to it in Handles[0]. The second through fourth calls to CreateEvent merely create new handles to the same auto-reset event because the name matches an existing event. The second loop sets that same event four times. And then the WaitForMultipleObjects asks to wait for all of the handles to be signaled. But since all four handles refer to the same object, it's being asked to wait until the event has reached the state where the wait can complete four times simultaneously. (Huh?)

Read more: The old new thing

Posted via email from Jasper-net

0 comments: