How to continue sending/reading messages from named pipes / streams

六月ゝ 毕业季﹏ 提交于 2019-12-01 06:31:40
Pavel Krymets

StreamReader closes stream passed to it after disposal, and you are disposing StreamReader in the end of using (StreamReader sr = new StreamReader(_pipeClient)) block.

You can create StreamReader at class level in constructor, and use it in ReadMessages method

  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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!