locked
"Timeouts are not supported on this stream." Named Pipes .NET3.5 RRS feed

  • Question

  • I am creating a Named Pipe to have my 64 bit app send requests to my 32 bit app (it must be this way, trust me).

    The Listener part gets to the point where it's doing a StreamReader.ReadLine() and that's where it pauses to wait for input.

    What I'd like is to set a timeout so the app can do the following

    1. check if the client has disconnected
    2. Terminate the program if he has
    3. go back to the readline if he has not.

    my app will likely spend 90% of its time at this readline command, and so far, closing the client app without notifying the listener does not toss any exceptions.  It just waits.  Forever.

    Is there a way to detect named pipes clients disconnecting?  One might think there was an OnConnect and OnDisconnect event hook, but there is not.


    I'd rather live with false hope than with false despair.

    Friday, December 12, 2014 11:41 PM

Answers

  • Set a read timeout and poll the NamedPipeClientStream.IsConnected flag when a timeout occurs.

    A Read Timeout will cause reads that are idle for the timeout duration to throw InvalidOperationException

    If you are not reading, and want to detect disconnections, call this method on a worker thread for the lifetime of your pipe connection.

    while(pipe.IsConnected && !isPipeStopped) //use a flag so that you can manually stop this thread
    {
        System.Threading.Thread.Current.Sleep(500);
    }
    
    if(!pipe.IsConnected)
    {
        //pipe disconnected
        NotifyOfDisconnect();
    }
    Monday, December 15, 2014 6:53 AM