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

后端 未结 1 1931
一生所求
一生所求 2021-01-13 23:11

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). The server app creates an instance of

相关标签:
1条回答
  • 2021-01-13 23:11

    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);
        }
    
    0 讨论(0)
提交回复
热议问题