NamedPipeServerStream.WaitForConnection Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Väntar på att en klient ska ansluta till det här NamedPipeServerStream objektet.
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 ()
- Attribut
Undantag
En röranslutning har redan upprättats.
-eller-
Rörhandtaget har inte ställts in.
Röret är stängt.
Röranslutningen har brutits.
Exempel
I följande exempel visas en metod för att skicka en sträng från en överordnad process till en underordnad process med hjälp av namngivna pipes. Det här exemplet skapar ett NamedPipeServerStream objekt i en överordnad process. Det här objektet har värdet PipeDirectionOut, som sedan blockeras tills ett NamedPipeClientStream objekt upprättar en anslutning till NamedPipeServerStream objektet. Det här exemplet är en del av ett större exempel som tillhandahålls för klasserna NamedPipeServerStream och NamedPipeClientStream .
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
Kommentarer
Om du anropar den NamedPipeServerStream här metoden blockeras objektet tills en klient ansluter.