NamedPipeServerStream.WaitForConnection Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com 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)

[SecurityCriticalAttribute]
public void WaitForConnection()

Exception Condition
InvalidOperationException

A pipe connection has already been established.

-or-

The pipe handle has not been set.

ObjectDisposedException

The pipe is closed.

IOException

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...");
            pipeServer.WaitForConnection();

            Console.WriteLine("Client connected.");
            try
            {
                // Read user input and send that to the client process.
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    Console.Write("Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }
            }
            // 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
Show: