远程收盘后,保持管道开启的最佳方法管道、方法

2023-09-04 01:20:19 作者:虚伪的泪水

使用本教程我想出了$低于C $ C。我的客户经常遇到。其通过点击激活,并且可能可以在同一时刻在特定情况下被推出两次。我而另一个客户端打开,导致管道在纤薄几毫秒内被关闭担心一个客户端可能会关闭。请告诉我要保持管道打开?最好的办法

 的静态公共无效ThreadStartServer()
    {
        而(真)
        {
            使用(NamedPipeServerStream pipeStream =新NamedPipeServerStream(mytestpipe))
            {
                Console.WriteLine([服务器]管道创建{0},pipeStream.GetHash code());

                pipeStream.WaitForConnection();
                Console.WriteLine([服务器]管道连接建立);

                使用(StreamReader的SR =新的StreamReader(pipeStream))
                {
                    串温度;
                    而((TEMP = sr.ReadLine())!= NULL)
                    {
                        Console.WriteLine({0}:{1},DateTime.Now,温度);
                    }
                }
            }
        }
 

解决方案

请您管道服务器多线程,有一个线程专用于倾听。请参阅these answers:

12月13日收盘点评

Using this tutorial i came up with the code below. My client is ran frequently. Its activated via clicks and possibly can be launched twice at the same moment in certain circumstance. I am worried one client may close while another client opens which causes the pipe to be closed in that slim few milliseconds. Whats the best way to keep the pipe open?

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

解决方案

Make your pipe server multi-threaded, with one thread dedicated to listening. See these answers: