问题描述
我有一个线程可以像这样创建一个命名管道:
I have a thread which creates a named pipe like this:
CreateNamedPipe('\\\\.\\pipe\\dirwatcher', PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1, 1, 0, null);
然后我生成另一个名为"poller"的线程,该线程具有监视文件.我想让它等待这个管道,所以我可以中断无限轮询.
I then spawn another thread called "poller" and this one has watches files. I want to make it wait for this pipe though, so I can interrupt the infinite poll.
我在此轮询线程中创建管道,如下所示:
I create the pipe in this poller thread like this:
pipe = CreateFile('\\\\.\\pipe\\dirwatcher', GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, null);
然后我尝试像这样在管道上等待:
I then try to wait on this pipe like this:
在这种情况下,LP_HANDLES是一个只有一个元素pipe
的数组:
For this case LP_HANDLES is an array with just one element, pipe
:
rez_wait = WaitForMultipleObjectsEx(LP_HANDLES.length, LP_HANDLES, false, INFINITE, true);
console.log('rez_wait:', rez_wait);
但是,这根本没有等待,并立即返回0.在创建命名管道并连接到它的过程中,我都使用了重叠标志.有人知道如何解决这个问题吗?
However this doesn't wait at all, and immeidately return 0. I used overlapped flag in both creating the named pipe and connecting to it. Does anyone know how to fix this?
这是我的代码,它是js-ctypes:
Here is my code specifically, it is js-ctypes:
创建管道: https://github.com/Noitidart/jscFileWatcher/blob/master /DirectoryWatcherWorkerSubscript.js#L91-L99
在轮询线程中获取管道: https://github.com/Noitidart/jscFileWatcher/blob/master /DirectoryWatcherPollWorker.js#L53-L64
Getting the pipe in poller thread:https://github.com/Noitidart/jscFileWatcher/blob/master/DirectoryWatcherPollWorker.js#L53-L64
在管道上等待: https://github.com/Noitidart/jscFileWatcher/blob/master/DirectoryWatcherPollWorker .js#L89
谢谢
推荐答案
您不能使用 WaitForMultipleObjectsEx
,如备注所述:
You cannot wait for pipes using WaitForMultipleObjectsEx
as stated within the remarks:
- 更改通知
- 控制台输入
- 事件
- 内存资源通知
- Mutex
- 过程
- 信号量
- 线程
- 等待计时器
- Change notification
- Console input
- Event
- Memory resource notification
- Mutex
- Process
- Semaphore
- Thread
- Waitable timer
您必须使用 PeekNamedPipe
来完成这项工作(如果管道为空,它不会等待),或者您只需在管道上执行阻塞ReadFile
即可.
前段时间我遇到了相同的问题,我通过等待WaitForSingleObject
事件来解决该问题,如果我在管道中放了东西,我会设置该事件.我必须这样做,因为我有多个管道,并且只有一个由事件唤醒的读取器线程",并且使用PeekNamedPipe
来检查哪个管道包含数据(并且我不想创建多个读取器线程"在ReadFile
调用中被屏蔽).
I've encountered the same problem a while ago and I've solved it by waiting for an event with WaitForSingleObject
which I set if I've put something into the pipe. I had to do this since I had several pipes and only one "reader thread" which woke up by the event and the used PeekNamedPipe
to check which pipe contains data (and I didn't want to create several "reader threads" which are blocked within a ReadFile
call).
无论如何,对于我来说,您可以使用WaitForMultipleObjects
等待几乎所有内容,这很奇怪,除了管道(这非常令人讨厌!).
Anyway to me it is quite strange that you can wait for almost anything using WaitForMultipleObjects
except pipes (which is very annoying!).
这篇关于CreateNamedPipe和WaitForMultipleObjectsEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!