FindFirstChangeNotification 多次触发
我有一个简单的应用程序,它生成两个线程,并为一个线程分配处理某些文件并将结果保存到另一个线程的任务,而另一个线程检索有关其父进程的信息。
我正在使用一些手动重置事件和 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
是个好主意吗?
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FindFirstChangeNotification
返回的句柄无法传递给ResetEvent
。如果您想等待另一个事件,请使用FindNextChangeNotification。如果您已完成,请使用 FindCloseChangeNotification。文档暗示了这一点:“如果函数成功,返回值是查找更改通知对象的句柄。”它返回查找更改通知对象的句柄,而不是事件。因此,它是
ResetEvent
的无效参数。The handle returned by
FindFirstChangeNotification
cannot 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
.