I have a simple application which spawns two threads and assigns one with a task of processing some file and saving the result to another one, while the other thread retrieves information about it’s parent process.
I’m using some manual reset events and a FindFirstChangeNotification function. The primary thread enters an infinite loop, inside calling WaitForMultipleObjectsEx.
Here’s the snippet:
while(TRUE){
waitResult = WaitForMultipleObjectsEx(4, eventObjectHandles, FALSE, 5000, FALSE);
switch(waitResult){
case WAIT_OBJECT_0 + 0:
_tprintf(_T("\nThread with ID: %d has finished processing the poem.\n"), threadIds[0]);
_tprintf(_T("Output file path: %s\n"), thread_xyz_param.outputPath);
ResetEvent(eventObjectHandles[0])
break;
case WAIT_OBJECT_0 + 1:
ResetEvent(eventObjectHandles[1]);
break;
case WAIT_OBJECT_0 + 2:
_tprintf(_T("Error in thread with ID: %d!\n"), threadIds[0]);
ResetEvent(eventObjectHandles[2]);
break;
case WAIT_OBJECT_0 + 3:
_tprintf(_T("Error in thread with ID: %d!\n"), threadIds[1]);
ResetEvent(eventObjectHandles[3]);
break;
}
GetExitCodeThread(threadHandles[0], &firstThreadStatus);
GetExitCodeThread(threadHandles[1], &secondThreadStatus);
if((firstThreadStatus != STILL_ACTIVE) && (secondThreadStatus != STILL_ACTIVE)){
break;
}
}
Problem is that the the FindFirstChangeNotification function is signalling multiple times, even though I write to the output file only once.
Is it a good idea to call FindCloseChangeNotification instead of ResetEvent?
Thanks in advance!
The handle returned by
FindFirstChangeNotificationcannot be passed toResetEvent. If you want to wait for another event, useFindNextChangeNotification. If you are finished with it, then useFindCloseChangeNotification.This is implied by the documentation: “If the function succeeds, the return value is a handle to a find change notification object.” It returns a handle to a find change notification object, not an event. Therefore, it is an invalid parameter to
ResetEvent.