简体   繁体   English

C#命名管道流readline挂起

[英]C# Named pipe stream readline hangs

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(); 我有命名管道的客户机/服务器结构,问题出在某个随机点,经过一段时间的工作,它只是挂在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? 有任何想法吗?

Client side sometimes do: 客户端有时会这样做:

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

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. 在运行X个小时后,它将在ReadLine停止,没有任何错误。 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. 如果您读取所有数据,似乎会发生这种情况。

If you do a "Peek()" and check for a value above 0 before you read you should be able to fix this. 如果您执行“ Peek()”并在读取前检查大于0的值,则应该可以解决此问题。

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

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

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