FindFirstChangeNotification 多次触发

发布于 2024-12-10 11:09:01 字数 1526 浏览 0 评论 0原文

我有一个简单的应用程序,它生成两个线程,并为一个线程分配处理某些文件并将结果保存到另一个线程的任务,而另一个线程检索有关其父进程的信息。
我正在使用一些手动重置事件和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-12-17 11:09:01

FindFirstChangeNotification 返回的句柄无法传递给 ResetEvent。如果您想等待另一个事件,请使用FindNextChangeNotification。如果您已完成,请使用 FindCloseChangeNotification。

文档暗示了这一点:“如果函数成功,返回值是查找更改通知对象的句柄。”它返回查找更改通知对象的句柄,而不是事件。因此,它是 ResetEvent 的无效参数。

The handle returned by FindFirstChangeNotification cannot be passed to ResetEvent. If you want to wait for another event, use FindNextChangeNotification. If you are finished with it, then use FindCloseChangeNotification.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文