打开和关闭管道

时间:2014-08-10 16:37:14

标签: c# pipe memory-efficient

如果我每秒呼叫服务器管道10次,我应该在呼叫之间关闭管道还是以某种方式让它们保持打开状态?

到目前为止,这是我的代码:

    public static void WaitForConnectionCallBack(IAsyncResult iar)
    {
        try
        {
            // Get the pipe
            pipeServer = (NamedPipeServerStream)iar.AsyncState;
            // End waiting for the connection
            pipeServer.EndWaitForConnection(iar);
            byte[] imageData = new byte[100000];
            pipeServer.Read(imageData, 0, imageData.Length);

            // Pass message back to calling form
            PipeMessage.Invoke(imageData);

            // Kill original sever and create new wait server
            //pipeServer.Close();
            //pipeServer = null;

            //PipeServer.PipeMessage -= new DelegateMessage(PipesMessageHandler);
            pipeServer.Disconnect();
            pipeServer.Close();
            pipeServer.Dispose();
            pipeServer = null;

            pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In,       1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

            // Recursively wait for the connection again and again....
            //System.Threading.Thread.Sleep(1000);
            pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
        }
        catch
        {
            return;
        }
    }

0 个答案:

没有答案