NamedPipeServerStream.WaitForConnection 方法


等待客户端连接到该 NamedPipeServerStream 对象。Waits for a client to connect to this NamedPipeServerStream object.

 void WaitForConnection();
public void WaitForConnection ();
public void WaitForConnection ();
member this.WaitForConnection : unit -> unit
Public Sub WaitForConnection ()


已建立管道连接。A pipe connection has already been established.

- 或 --or-

尚未设置管道句柄。The pipe handle has not been set.

管道已关闭。The pipe is closed.

管道连接已中断。The pipe connection has been broken.


下面的示例演示了使用命名管道将字符串从父进程发送到子进程的方法。The following example demonstrates a method to send a string from a parent process to a child process using named pipes. 此示例在父进程中创建一个 NamedPipeServerStream 对象。This example creates a NamedPipeServerStream object in a parent process. 此对象的 PipeDirection 值为 Out,然后在 NamedPipeClientStream 对象与 NamedPipeServerStream 对象建立连接之前进行阻止。This object has a PipeDirection value of Out, which then blocks until a NamedPipeClientStream object establishes a connection to the NamedPipeServerStream object. 此示例是为 NamedPipeServerStreamNamedPipeClientStream 类提供的更大示例的一部分。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);
Imports System.IO
Imports System.IO.Pipes

Class PipeServer

    Shared Sub Main()
        Dim pipeServer As New NamedPipeServerStream("testpipe", PipeDirection.Out)

        Console.WriteLine("NamedPipeServerStream object created.")

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

        Console.WriteLine("Client connected.")
            'Read user input and send that to the client process.
            Dim sw As New StreamWriter(pipeServer)
            sw.AutoFlush = True
            Console.Write("Enter Text: ")
        Catch ex As IOException
            ' Catch the IOException that is raised if the pipe is broken
            ' or disconnected
            Console.WriteLine("ERROR: {0}", ex.Message)
        End Try
    End Sub
End Class


调用此方法会导致 NamedPipeServerStream 对象在客户端连接之前被阻止。Calling this method causes the NamedPipeServerStream object to block until a client connects.