c - FindFirstChangeNotification 触发多次

标签 c multithreading winapi events wait

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

相关文章:

windows - 如何为 Win32 应用程序中的所有窗口设置默认字体?

c - 哪一个更好?做{}而(0);或转到 xy;

c - 存在哪些 C GNU 主义?

我能否确保某个线程始终在没有信号量的情况下最后工作?

c - pjsip pj_timer_heap_schedule 崩溃

c - 以下代码中是否存在临界区?

c++ - 线程数组并试图将多个参数传递给函数不起作用?

c - 解锁一个已经解锁的线程

windows - 对注册表的更改不生效

winapi - 错误 C1083 : cannot open include file: 'winsock2.h' : No such file or directory