NamedPipeServerStream.WaitForConnection Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Waits for a client to connect to this NamedPipeServerStream object.

Namespace:   System.IO.Pipes
Assembly:  System.Core (in System.Core.dll)

public void WaitForConnection()

Exception Condition

A pipe connection has already been established.


The pipe handle has not been set.


The pipe is closed.


The pipe connection has been broken.

Calling this method causes the NamedPipeServerStream object to block until a client connects.

The following example demonstrates a method to send a string from a parent process to a child process using named pipes. This example creates a NamedPipeServerStream object in a parent process. This object has a PipeDirection value of Out, which then blocks until a NamedPipeClientStream object establishes a connection to the NamedPipeServerStream object. This example is part of a larger example provided for the NamedPipeServerStream and NamedPipeClientStream classes.

using System;
using System.IO;
using System.IO.Pipes;

class PipeServer
    static void Main()
        using (NamedPipeServerStream pipeServer =
            new NamedPipeServerStream("testpipe", PipeDirection.Out))
            Console.WriteLine("NamedPipeServerStream object created.");

            // Wait for a client to connect
            Console.Write("Waiting for client connection...");

            Console.WriteLine("Client connected.");
                // Read user input and send that to the client process.
                using (StreamWriter sw = new StreamWriter(pipeServer))
                    sw.AutoFlush = true;
                    Console.Write("Enter text: ");
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
                Console.WriteLine("ERROR: {0}", e.Message);

.NET Framework
Available since 3.5
Return to top