NamedPipeServerStream.WaitForConnection 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
等候用戶端連接到這個 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 父進程中建立物件。 這個物件具有 PipeDirection 的值 Out,然後會封鎖物件,直到 NamedPipeClientStream 物件建立與 NamedPipeServerStream 對象的連接為止。 這個範例是 針對和 NamedPipeClientStream 類別提供之較大範例的NamedPipeServerStream一部分。
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 對象封鎖,直到用戶端連線為止。