【发布时间】:2009-12-17 04:02:38
【问题描述】:
当以异步模式打开的 System.IO.Pipe.NamedPipeServerStream 有更多数据可供读取时,我需要找到一种通知方式 - WaitHandle 是理想的选择。我不能简单地使用 BeginRead() 来获取这样的句柄,因为我可能会被另一个想要写入管道的线程发出信号 - 所以我必须释放管道上的锁并等待写入完成,并且 NamedPipeServerStream 没有 CancelAsync 方法。我还尝试调用 BeginRead(),然后在线程收到信号时调用管道上的 win32 函数 CancelIO,但我认为这不是一个理想的解决方案,因为如果在数据到达和处理时调用 CancelIO,它将被删除 - 我仍然希望保留这些数据,但在写入之后稍后处理它。我怀疑 win32 函数 PeekNamedPipe 可能有用,但我想避免使用它不断轮询新数据。
如果上面的文字有点不清楚,这大概是我想做的事情......
NamedPipeServerStream pipe;
ManualResetEvent WriteFlag;
//initialise pipe
lock (pipe)
{
//I wish this method existed
WaitHandle NewDataHandle = pipe.GetDataAvailableWaithandle();
Waithandle[] BreakConditions = new Waithandle[2];
BreakConditions[0] = NewDataHandle;
BreakConditions[1] = WriteFlag;
int breakcode = WaitHandle.WaitAny(BreakConditions);
switch (breakcode)
{
case 0:
//do a read on the pipe
break;
case 1:
//break so that we release the lock on the pipe
break;
}
}
【问题讨论】:
标签: c# .net asynchronous named-pipes overlapped-io