c# - 远程关闭后保持管道打开的最佳方法

标签 c# .net pipe named-pipes

使用 this tutorial我想出了下面的代码。我的客户经常跑。它通过点击激活,在某些情况下可能会同时启动两次。我担心一个客户端可能会在另一个客户端打开时关闭,这会导致管道在短短的几毫秒内关闭。保持管道畅通的最佳方法是什么?

    static public void ThreadStartServer()
    {
        while (true)
        {
            using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
            {
                Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());

                pipeStream.WaitForConnection();
                Console.WriteLine("[Server] Pipe connection established");

                using (StreamReader sr = new StreamReader(pipeStream))
                {
                    string temp;
                    while ((temp = sr.ReadLine()) != null)
                    {
                        Console.WriteLine("{0}: {1}", DateTime.Now, temp);
                    }
                }
            }
        }

最佳答案

使您的管道服务器成为多线程的,一个线程专用于监听。参见 these answers :

关于c# - 远程关闭后保持管道打开的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1689360/

相关文章:

c# - 包括派生模型相关类

c# - 线程(这些线程是否自行退出或中止?)

c# - 显示 DataTable 的值

c# - 在数以千计的 xml 文件中搜索

c - 在 C 语言中使用管道

c# - 使用C#识别 “GridViewCellEditEndedEvent”中的选定行

c# - Orchard 的 IContentManager.BuildDisplay 方法中的 groupId 参数是干什么用的?

c# - 如何动态更新/添加项目到 IObservable<int> 中?

c - pipe2(...) vs pipe() + fcntl(...),为什么不同?

c - 通过 C 中的 pipe/dup2 向子进程发送命令