locked
Pipe is being closed Exception RRS feed

  • Question

  • Sometimes, on some machines often and on some seldom, the clients which are using my program are getting a "pipe is being closed" exception. This happens on the NamedPipeServerStream that is on .WaitForConnection(). After that, the Application totally crashes and releases a windows exception. This happens when a NamedPipeClientStream transfers information to the standalone application.

    main Funktionality: I wrote several Tools (Office Toolbars, a Service, a standalone .net Application, and a litle starter exe) that communicate together with NamedPipes. The Service runs a NamedPipeServerStream that is always open (in state .WaitForConnection();) and the Standalone Application has a NamedPipeServerStream too. The Toolbars and starter .exe communicate with the service. The Service then with the Standalone Application.

    what kind of problems can release the pipe is being closed Exception? Is it possible that the server sends information to the Standalone Application but closes the stream to early because the Standalone Application is not ready or something? on each NamedPipeClientStream i do a waitforpipedrain if pipeClient.IsConnected befor i close the pipeclient..

    Here an Example of a clientstream

    using (NamedPipeClientStream pipeClient =
                            new NamedPipeClientStream(".", pipename, PipeDirection.Out))
                    {
                        // Wait for a client to connect
                        try
                        {                            
                            pipeClient.Connect(3000);
    
                            // send params to the form
                            using (StreamWriter sw = new StreamWriter(pipeClient))
                            {
                                sw.AutoFlush = true;
                                sw.WriteLine(sendtext);
                            }
                        }
                        // Catch the IOException that is raised if the pipe is
                        // broken or disconnected.
                        catch (Exception e)
                        {
                            if (sid != "")
                            {
                                connections.Remove(conName);
                            }
                            eventLog1.WriteEntry("SendText Fehler 1 " + e.Message);
                        }
                        finally
                        {
                            if (pipeClient.IsConnected)
                            {
                                pipeClient.WaitForPipeDrain();
                            }
                            pipeClient.Close();
                            pipeClient.Dispose();
                        }
    

    Example of an pipeserver (that runs in a seperad thread)

    NamedPipeServerStream pipeServer;
            PipeSecurity pipe_security = CreateSystemIoPipeSecurity();
            do
                string pipename = global::TOfficeCenter.Properties.Settings.Default.pipename;
                string aText = "";
                pipeServer = new NamedPipeServerStream(pipename, PipeDirection.In, ONE_INSTANCE, PipeTransmissionMode.Byte,
                                                PipeOptions.None, IN_BUF_SIZE, OUT_BUF_SIZE, pipe_security);
                try
                {
                    // Verbindung zu TOfficeCenter.exe aufbauen
                    try
                    {
                        IsWaiting = true;
                        pipeServer.WaitForConnection();
                        IsWaiting = false;
    
                        using (StreamReader sr = new StreamReader(pipeServer))
                        {
                            string temp;
                            while ((temp = sr.ReadLine()) != null)
                            {
                                aText = aText + temp;
                            }
                        }
    
                        try
                        {
                            if (aText == "")
                            {
                                empfang(null);
                            }
                            else
                            {
                                if (aText != "KillPipe")
                                {  // XML empfangen
                                    XmlDocumentTC xml = new XmlDocumentTC();
                                    xml.LoadXml(aText);
                                    empfang(xml);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            empfang(null);
                        }
                    }
                    catch 
            {...........
            }       
                }
    
                catch (Exception e)
                {...........
                }
    
            } while (running);
    
            pipeServer.Close();
    

    thanks for help

    Tuesday, February 7, 2012 2:25 PM

Answers

  • It's possible that i finally found the problem..

    I found out that after this code:

                            using (StreamWriter sw = new StreamWriter(pipeClient))
                            {
                                sw.AutoFlush = true;
                                sw.WriteLine(sendtext);
                            }

    the pipeClient.IsConnected(); returns directly false, so that it never comes to the WaitForPipeDrain. I Now did it like that and hope that the client does not close the connection before the server is finished with reading..

    using (StreamWriter sw = new StreamWriter(pipeClient)) { sw.AutoFlush = true; sw.WriteLine(sendtext);
    pipeClient.WaitForPipeDrain(); }

    Do You Think that could have solved the problem? Since i did it, i never got the error on two test-machines. But the Errors anyway happened seldom..

    • Marked as answer by Paul Zhou Thursday, February 23, 2012 9:49 AM
    Wednesday, February 8, 2012 2:57 PM

All replies

  • It's possible that i finally found the problem..

    I found out that after this code:

                            using (StreamWriter sw = new StreamWriter(pipeClient))
                            {
                                sw.AutoFlush = true;
                                sw.WriteLine(sendtext);
                            }

    the pipeClient.IsConnected(); returns directly false, so that it never comes to the WaitForPipeDrain. I Now did it like that and hope that the client does not close the connection before the server is finished with reading..

    using (StreamWriter sw = new StreamWriter(pipeClient)) { sw.AutoFlush = true; sw.WriteLine(sendtext);
    pipeClient.WaitForPipeDrain(); }

    Do You Think that could have solved the problem? Since i did it, i never got the error on two test-machines. But the Errors anyway happened seldom..

    • Marked as answer by Paul Zhou Thursday, February 23, 2012 9:49 AM
    Wednesday, February 8, 2012 2:57 PM
  • see this:

    http://msdn.microsoft.com/en-us/library/system.io.pipes.pipestream.waitforpipedrain.aspx

    the WaitForPipeDrain method waits for the other end of the pipe to read all sent bytes.


    Hard hard work, Day day up!

    Friday, February 10, 2012 9:56 AM