如何解锁ConnectNamedPipe和ReadFile? [C#]
我有一个类(NamedPipeManager),它有一个线程(PipeThread),它等待使用(ConnectNamedPipe)的NamedPipe连接,然后读取(ReadFile) - 这些是阻塞调用(不重叠) - 然而,当我想要时,有一个点取消阻止它们 - 例如,当调用类尝试停止 NamedPipeManager 时...
我怎样才能中断它?使用Thread.abort?线程.中断?有没有正确的方法来处理这个问题? 请参阅下面的代码,它说明了我当前的情况
main()
{
NamedPipeManager np = new NamedPipeManager();
... do stuff ...
... do stuff ...
np.Stop(); // at this point I want to stop waiting on a connection
}
class NamedPipeManager
{
private Thread PipeThread;
public NamedPipeManager
{
PipeThread = new Thread(new ThreadStart(ManagePipes));
PipeThread.IsBackground = true;
PipeThread.Name = "NamedPipe Manager";
PipeThread.Start();
}
private void ManagePipes()
{
handle = CreateNamedPipe(..., PIPE_WAIT, ...);
ConnectNamedPipe(handle, null); // this is the BLOCKING call waiting for client connection
ReadFile(....); // this is the BLOCKING call to readfile after a connection has been established
}
public void Stop()
{
/// This is where I need to do my magic
/// But somehow I need to stop PipeThread
PipeThread.abort(); //?? my gut tells me this is bad
}
};
那么,在函数 Stop() 中 - 我如何优雅地解除对 ConnectNamedPipe(...) 或 ReadFile(...) 的调用?
任何帮助将不胜感激。 谢谢,
I have a class (NamedPipeManager) which has a thread (PipeThread) that waits for a NamedPipe connection using (ConnectNamedPipe) and then reads (ReadFile) - these are blocking calls (not-overlapped) - however there comes a point when I want to unblock them - for example when the calling class tries to stop the NamedPipeManager...
How can I interupt it? Using Thread.abort? Thread.interrupt? Is there a proper way to handle this?
Refer to the code below which illustrates my current situation
main()
{
NamedPipeManager np = new NamedPipeManager();
... do stuff ...
... do stuff ...
np.Stop(); // at this point I want to stop waiting on a connection
}
class NamedPipeManager
{
private Thread PipeThread;
public NamedPipeManager
{
PipeThread = new Thread(new ThreadStart(ManagePipes));
PipeThread.IsBackground = true;
PipeThread.Name = "NamedPipe Manager";
PipeThread.Start();
}
private void ManagePipes()
{
handle = CreateNamedPipe(..., PIPE_WAIT, ...);
ConnectNamedPipe(handle, null); // this is the BLOCKING call waiting for client connection
ReadFile(....); // this is the BLOCKING call to readfile after a connection has been established
}
public void Stop()
{
/// This is where I need to do my magic
/// But somehow I need to stop PipeThread
PipeThread.abort(); //?? my gut tells me this is bad
}
};
So, in function Stop() - how would I gracefully unblock the call to ConnectNamedPipe(...) or ReadFile(...)?
Any help would be appreciated.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我尝试通过以下方式中断
ConnectNamedPipe
,它似乎可以在 VC6.0、WinXP 上运行DeleteFile("\\\\.\\pipe\\yourpipehere");
所以只需指定名称,而不是句柄。
It seems to be working on VC6.0, WinXP if I try to interrupt
ConnectNamedPipe
byDeleteFile("\\\\.\\pipe\\yourpipehere");
So just specify name, not handle.
从 Windows Vista 开始,有一个 CancelSynchronousIO 操作可用于线程。我认为没有 C# 包装器,因此您需要使用 PInvoke 来调用它。
在 Vista 之前,确实没有办法优雅地执行此类操作。我建议不要使用线程取消(这可能有效,但不够优雅)。最好的方法是使用重叠 IO。
Starting with Windows Vista, there is a CancelSynchronousIO operation available for threads. I don't think there is a C# wrapper for it, so you would need to use PInvoke to call it.
Before Vista, there isn't really a way to perform such an operation gracefully. I would advise against using thread cancellation (which might work, but doesn't qualify as graceful). Your best approach is to use overlapped IO.
最近我遇到一种情况,我无法使用异步重叠IO。我被困在 ConnectNamedPipe 中的服务器端。为了解锁线程并释放资源,我必须瞬间连接到与客户端相同的管道。
这对我来说非常有效。
要取消阻止ReadFile,需要连接并写入管道。预期效果相同。
Recently I was in a situation, I could not use the Async Overlapped IO. I was stuck on the server side within ConnectNamedPipe. To unlock the thread and free resources, I had to connect to the same pipe as a client for a split second.
This worked for me very well.
To unblock ReadFile one needs to connect and write to the pipe. Same effect epected.