NamedPipeServerStream.WaitForConnection 方法

定义

等待客户端连接到该 NamedPipeServerStream 对象。

public:
 void WaitForConnection();
public void WaitForConnection ();
[System.Security.SecurityCritical]
public void WaitForConnection ();
member this.WaitForConnection : unit -> unit
[<System.Security.SecurityCritical>]
member this.WaitForConnection : unit -> unit
Public Sub WaitForConnection ()
属性

例外

已建立管道连接。

- 或 -

尚未设置管道句柄。

管道已关闭。

管道连接已中断。

示例

以下示例演示了使用命名管道将字符串从父进程发送到子进程的方法。 此示例在 NamedPipeServerStream 父进程中创建对象。 此对象的值OutPipeDirection ,然后会阻止该对象,直到NamedPipeClientStream对象与对象NamedPipeServerStream建立连接。 此示例是为 NamedPipeServerStreamNamedPipeClientStream 类提供的更大示例的一部分。

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

        Console.WriteLine("Client connected.")
        Try
            'Read user input and send that to the client process.
            Dim sw As New StreamWriter(pipeServer)
            sw.AutoFlush = True
            Console.Write("Enter Text: ")
            sw.WriteLine(Console.ReadLine())
        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 对象被阻止,直到客户端连接。

适用于