c# - 如何解锁 ConnectNamedPipe 和 ReadFile? [C#]

标签 c# c++ pinvoke pipe named-pipes

我有一个类 (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/

相关文章:

c# - Windows 8 Metro-UI 的控件样式和模板

C#如何引用不同版本依赖的依赖

C# 等待 lambda 回调完成

.net - 我可以强制 MSTest 为每次测试运行使用新进程吗?

c# - 在 Linux 上使用 Mono 进行 native P/调用 : DllNotFound

c# - 如何通过禁用 Name Mangling 来调用实例方法

C# LINQ Xml查询混淆

c++ - 用结构体数据减去指针

java - 谷歌文档如何在不使用 Flash 查看器的情况下显示我的 .PPT 文件?

c++ - C++性能测量中的一些问题