我有一个类 (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(...) 的调用?
如有任何帮助,我们将不胜感激。 谢谢,
最佳答案
如果我尝试通过以下方式中断 ConnectNamedPipe
,它似乎可以在 VC6.0 和 WinXP 上运行
DeleteFile("\\\\.\\pipe\\yourpipehere");
所以只指定名称,而不是句柄。
关于c# - 如何解锁 ConnectNamedPipe 和 ReadFile? [C#],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1353263/