我有一个简单的应用程序,它产生两个线程并分配一个任务来处理一些文件并将结果保存到另一个线程,而另一个线程检索有关其父进程的信息。
我正在使用一些手动重置事件和一个 FindFirstChangeNotification
函数。主线程进入无限循环,内部调用WaitForMultipleObjectsEx
。
这是片段:
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;
}
}
问题是 FindFirstChangeNotification
函数发出多次信号,即使我只写入输出文件一次。
调用 FindCloseChangeNotification
而不是 ResetEvent
是个好主意吗?
提前致谢!
最佳答案
FindFirstChangeNotification
返回的句柄无法传递给 ResetEvent
。如果您想等待另一个事件,请使用 FindNextChangeNotification
。如果您已完成它,请使用 FindCloseChangeNotification
。
这由 the documentation 暗示:“如果函数成功,返回值是查找更改通知对象的句柄。”它返回查找更改通知对象的句柄,而不是事件。因此,它是 ResetEvent
的无效参数。
关于c - FindFirstChangeNotification 触发多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7774942/