如何继续从命名管道/流发送/读取消息 [英] How to continue sending/reading messages from named pipes / streams

查看:558
本文介绍了如何继续从命名管道/流发送/读取消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要教自己使用的管道,我有两个应用程序,一个具有PipeServer类和一个与PipeClient类(如下图所示)。服务器应用程序创建PipeServer的实例并具有调用的WriteMessage方法文本框改变时的文本框。该客户端应用程序创建PipeClient的一个实例,设置MessageReadEvent来填充在给定的消息的文本框的方法,然后调用ReadMessages方法。

第一次ReadMessages方法被调用它到达sr.ReadLine(),并等待,直到接收到消息。后一条消息被接收到下一个调用sr.ReadLine()自然返回null并继续退出方法。

在这一点上,任何进一步的调用ReadMessages给了我一个异常说,在管道关闭。我不知道我明白为什么管是越来越封闭。我如何保持打开状态?当然,我没有创建一个管道的新实例为每封邮件我想送?

下面是我的PipeClient类。我可以加我的PipeServer类太多,如果这将是有益的,但​​我认为这个问题坐落在这里...

 公共委托无效MessageReadEventHandler(字符串消息);

    类PipeClient:IDisposable的
    {
        公共事件MessageReadEventHandler MessageReadEvent;

        NamedPipeClientStream _pipeClient;

        公共PipeClient(字符串pipeName)
        {
            _pipeClient =新NamedPipeClientStream(,pipeName,PipeDirection.In。);
            _pipeClient.Connect();
        }

        公共无效ReadMessages()
        {
            串温度;

            //是_pipeClient得到处置时,SR得到处理?
            //我不这么认为,但我不明白为什么我似乎无法
            //以下using语句运行后再次使用

            使用(StreamReader的SR =新的StreamReader(_pipeClient))
                而((TEMP = sr.ReadLine())!= NULL)
                    如果(MessageReadEvent!= NULL)
                        MessageReadEvent(临时);
        }

        公共无效的Dispose()
        {
            _pipeClient.Dispose();
        }
    }
 

解决方案

的StreamReader关闭流传递给它的处理后,和您在年底使用处置的StreamReader(StreamReader的SR =新的StreamReader(_pipeClient ))块。

您可以在构造函数中一流水平打造的StreamReader,并用它在ReadMessages方法

 公共PipeClient(字符串pipeName)
    {
        _pipeClient =新NamedPipeClientStream(,pipeName,PipeDirection.In。);
        _pipeClient.Connect();
        _streamReader =新的StreamReader(_pipeClient);
    }
  公共无效ReadMessages()
    {
        串温度;



            而((TEMP = _streamReader.ReadLine())!= NULL)
                如果(MessageReadEvent!= NULL)
                    MessageReadEvent(临时);
    }
 

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 the PipeServer and has a text box that calls the WriteMessage method when the text box changes. 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.

The first time the ReadMessages method is called it gets to the sr.ReadLine() and waits there until a message is received. After a message is received the next call to sr.ReadLine() naturally returns null and it continues to exit the method.

At this point, any further calls to ReadMessages gives me an exception saying the pipe is closed. 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. I can add my PipeServer class too if it would be helpful, but I think the issue is located in here...

    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.

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

这篇关于如何继续从命名管道/流发送/读取消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆