Connect Method
Collapse the table of content
Expand the table of content

NamedPipeClientStream.Connect Method ()

.NET Framework (current version)
 

Connects to a waiting server with an infinite time-out value.

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

public void Connect()

Exception Condition
InvalidOperationException

The client is already connected.

This method calls the Connect(Int32) method with an infinite time-out value.

This method waits for a pipe instance to become available. Connect might return before WaitForConnection is called from the NamedPipeServerStream object, but WaitForConnection will not return until Connect has returned.

Any data written to the pipe after a NamedPipeClientStream object has connected, but before the server has called WaitForConnection, will be available to the server following the call to WaitForConnection.

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 NamedPipeClientStream object in a child process, which then connects to a pipe on the local computer. The server example can be seen in the NamedPipeServerStream class. 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 PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient =
            new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }
        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}

.NET Framework
Available since 3.5
Return to top
Show:
© 2016 Microsoft