【问题标题】:Best way to keep a pipe open after a remote close远程关闭后保持管道打开的最佳方法
【发布时间】:2009-11-06 18:15:08
【问题描述】:

使用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);
                    }
                }
            }
        }

【问题讨论】:

  • 两个客户端如何连接到同一个管道?这能解决吗?

标签: c# .net pipe named-pipes


【解决方案1】:

使您的管道服务器具有多线程,其中一个线程专门用于监听。见theseanswers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多