简体   繁体   English

如何继续从命名管道/流发送/读取消息

[英]How to continue sending/reading messages from named pipes / streams

I'm teaching myself to use pipes and I have two apps, one with the PipeServer class and one with the PipeClient class (shown below). 我正在教自己使用管道,我有两个应用程序,一个使用PipeServer类,另一个使用PipeClient类(如下所示)。 The server app creates an instance of the PipeServer and has a text box that calls the WriteMessage method when the text box changes. 服务器应用程序创建PipeServer的实例,并具有一个文本框,在文本框更改时调用WriteMessage方法。 The client app creates an instance of the PipeClient, sets the MessageReadEvent to a method that fills in a textbox with the given message, and then calls the ReadMessages method. 客户端应用程序创建PipeClient的实例,将MessageReadEvent设置为使用给定消息填充文本框的方法,然后调用ReadMessages方法。

The first time the ReadMessages method is called it gets to the sr.ReadLine() and waits there until a message is received. 第一次调用ReadMessages方法时,它会进入sr.ReadLine()并在那里等待,直到收到消息。 After a message is received the next call to sr.ReadLine() naturally returns null and it continues to exit the method. 收到消息后,下一次调用sr.ReadLine()会自然返回null并继续退出该方法。

At this point, any further calls to ReadMessages gives me an exception saying the pipe is closed. 此时,对ReadMessages的任何进一步调用都会给我一个例外,说管道已关闭。 I'm not sure I understand why the pipe is getting closed. 我不确定我理解管道为什么会关闭。 How do I keep it open? 我该如何保持开放状态? Surely I don't have to create a new instance of a pipe for every message I want to send? 当然,我不必为每个要发送的消息创建一个新的管道实例?

Below is my PipeClient class. 下面是我的PipeClient类。 I can add my PipeServer class too if it would be helpful, but I think the issue is located in here... 如果它有用,我也可以添加我的PipeServer类,但我认为这个问题位于这里...

    public delegate void MessageReadEventHandler(string message);

    class PipeClient: IDisposable
    {
        public event MessageReadEventHandler MessageReadEvent;

        NamedPipeClientStream _pipeClient;

        public PipeClient(string pipeName)
        {
            _pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.In);
            _pipeClient.Connect();
        }

        public void ReadMessages()
        {
            string temp;

            // Is _pipeClient getting disposed when sr gets disposed??
            // I wouldn't think so but I don't understand why I can't seem
            // to use it again after the following using statement is run

            using (StreamReader sr = new StreamReader(_pipeClient))
                while ((temp = sr.ReadLine()) != null)
                    if(MessageReadEvent != null)
                        MessageReadEvent(temp);
        }

        public void Dispose()
        {
            _pipeClient.Dispose();
        }
    }

StreamReader closes stream passed to it after disposal, and you are disposing StreamReader in the end of using (StreamReader sr = new StreamReader(_pipeClient)) block. StreamReader关闭在处理后传递给它的流,并且在using (StreamReader sr = new StreamReader(_pipeClient))块的最后处理StreamReader。

You can create StreamReader at class level in constructor, and use it in ReadMessages method 您可以在构造函数中在类级别创建StreamReader,并在ReadMessages方法中使用它

  public PipeClient(string pipeName)
    {
        _pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.In);
        _pipeClient.Connect();
        _streamReader = new StreamReader(_pipeClient);
    }
  public void ReadMessages()
    {
        string temp;



            while ((temp = _streamReader.ReadLine()) != null)
                if(MessageReadEvent != null)
                    MessageReadEvent(temp);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM