C#命名管道流readline挂起 [英] C# Named pipe stream readline hangs

查看:131
本文介绍了C#命名管道流readline挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.我有命名管道的客户机/服务器结构,问题出在某个随机点,经过一段时间的工作,它只是挂在streamReader.ReadLine();上.它只是停止了,并且不会继续前进.我很困惑,我完全不知道发生了什么,以及实际上如何调试,为什么以及何时发生.有什么想法吗?

Im confused. I have client/server structure of named pipe, and the problem is at some random point, after a while of work it just hangs on the streamReader.ReadLine(); It just stops, and not going further. Im confused, I have no idea at all, what is going on, and actually how to debug that, why and when it happens. Any ideas?

客户端有时会做:

   private void Connect()
    {
        stream = new NamedPipeClientStream(".", "MP9", PipeDirection.InOut);
        try
        {
            stream.Connect(120);
        }
        catch (Exception e)
        {
            run = false;
            return;
        }

        //Initialising Readers/Writers
        sr = new StreamReader(stream);
        sw = new StreamWriter(stream);
    }

private string SendMessage(string msg)
{
    Debug.WriteLine("Will listen for message " + msg);

    string msgFrom = "";
    if (run)
    {
        string toReturn = "";
        lock (locker)
        {

            sw.WriteLine(msg); //Writing command to the pipes
            stream.WaitForPipeDrain(); //Waiting for another process to read the command
            msgFrom = sr.ReadLine(); //Reading
        }
    }
    return msgFrom;
}

我的服务器有一个线程,该线程侦听任何消息,并在需要时进行响应.在运行X个小时后,它将在ReadLine停止,没有任何错误.只是停在行上,并没有走得更远.但是应用程序仍在运行,它没有被挂起,只是这个侦听器线程似乎被挂起了.

And my server has a thread, that listens for any messages, and responds back if is needed. And after X hrs of running it will just stop at ReadLine, without errors or so. Just stops on the line, and not goes further. However app is still running, it is not hanged, just this listener thread seems hanged...

void Listen()
{
    try
    {
        stream.WaitForConnection();
        sw.AutoFlush = true;
        string messageTo = "";
        while (running) //Main loop of the thread
        {
            messageFrom = "";
            //HERE IT CAN JUST HANG AND NOT GO FURTHER....
            messageFrom = sr.ReadLine(); //Reading

            //populate message to with data, if is needed to respond

            sw.WriteLine(messageTo);
            stream.WaitForPipeDrain();
        }
    }
 }

推荐答案

如果您读取所有数据,似乎会发生这种情况.

This seems to happen if you read all the data.

如果您执行"Peek()"并在读取前检查大于0的值,则应该可以解决此问题.

If you do a "Peek()" and check for a value above 0 before you read you should be able to fix this.

stream.WaitForConnection();
    sw.AutoFlush = true;
    string messageTo = "";
    while (running) //Main loop of the thread
    {
        messageFrom = "";
        if(sr.Peek() > 0)
        {
           messageFrom = sr.ReadLine(); //Reading

          //populate message to with data, if is needed to respond

           sw.WriteLine(messageTo);
        }
        stream.WaitForPipeDrain();

这篇关于C#命名管道流readline挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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