AnonymousPipeServerStream 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在匿名管道周围公开流,该管道支持同步和异步读取和写入操作。
public ref class AnonymousPipeServerStream sealed : System::IO::Pipes::PipeStream
public sealed class AnonymousPipeServerStream : System.IO.Pipes.PipeStream
type AnonymousPipeServerStream = class
inherit PipeStream
Public NotInheritable Class AnonymousPipeServerStream
Inherits PipeStream
- 继承
示例
以下示例使用匿名管道将一个字符串从父进程发送到子进程。 本示例在父进程中创建
此示例适用于使用 AnonymousPipeServerStream 类的服务器进程。 有关整个代码示例(包括管道客户端和服务器的代码)请参阅 如何:使用匿名管道进行本地进程间通信。
//<snippet01>
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
class PipeServer
{
static void Main()
{
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "pipeClient.exe";
using (AnonymousPipeServerStream pipeServer =
new AnonymousPipeServerStream(PipeDirection.Out,
HandleInheritability.Inheritable))
{
Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
pipeServer.TransmissionMode);
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments =
pipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
// Send a 'sync message' and wait for client to receive it.
sw.WriteLine("SYNC");
pipeServer.WaitForPipeDrain();
// Send the console input to the client process.
Console.Write("[SERVER] Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("[SERVER] Error: {0}", e.Message);
}
}
pipeClient.WaitForExit();
pipeClient.Close();
Console.WriteLine("[SERVER] Client quit. Server terminating.");
}
}
//</snippet01>
'<snippet01>
Imports System.IO
Imports System.IO.Pipes
Imports System.Diagnostics
Class PipeServer
Shared Sub Main()
Dim pipeClient As New Process()
pipeClient.StartInfo.FileName = "pipeClient.exe"
Using pipeServer As New AnonymousPipeServerStream(PipeDirection.Out, _
HandleInheritability.Inheritable)
Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
pipeServer.TransmissionMode)
' Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments = pipeServer.GetClientHandleAsString()
pipeClient.StartInfo.UseShellExecute = false
pipeClient.Start()
pipeServer.DisposeLocalCopyOfClientHandle()
Try
' Read user input and send that to the client process.
Using sw As New StreamWriter(pipeServer)
sw.AutoFlush = true
' Send a 'sync message' and wait for client to receive it.
sw.WriteLine("SYNC")
pipeServer.WaitForPipeDrain()
' Send the console input to the client process.
Console.Write("[SERVER] Enter text: ")
sw.WriteLine(Console.ReadLine())
End Using
Catch e As IOException
' Catch the IOException that is raised if the pipe is broken
' or disconnected.
Console.WriteLine("[SERVER] Error: {0}", e.Message)
End Try
End Using
pipeClient.WaitForExit()
pipeClient.Close()
Console.WriteLine("[SERVER] Client quit. Server terminating.")
End Sub
End Class
'</snippet01>
注解
匿名管道有助于在子进程和父进程之间提供安全可靠的进程间通信。 该 AnonymousPipeServerStream 类使父进程能够从子进程发送或接收信息。
匿名管道是未命名的单向管道,通常在父进程和子进程之间传输数据。 匿名管道始终是本地管道;不能通过网络使用它们。 PipeDirection不支持值InOut,因为匿名管道定义为单向管道。
匿名管道不支持 PipeTransmissionMode.Message 读取模式。
匿名管道的客户端必须通过调用 GetClientHandleAsString 该方法从服务器端提供的管道句柄创建。 然后,在创建客户端进程时将字符串作为参数传递。 然后,在客户端进程中,它将作为参数传递给 AnonymousPipeClientStream 构造函数 pipeHandleAsString 。
对象 AnonymousPipeServerStream 必须使用该方法释放客户端句柄 DisposeLocalCopyOfClientHandle ,以便在客户端退出时收到通知。
构造函数
| 名称 | 说明 |
|---|---|
| AnonymousPipeServerStream() |
初始化 AnonymousPipeServerStream 类的新实例。 |
| AnonymousPipeServerStream(PipeDirection, HandleInheritability, Int32, PipeSecurity) |
使用指定的管道方向、可继承性模式、缓冲区大小和管道安全性初始化类的新实例 AnonymousPipeServerStream 。 |
| AnonymousPipeServerStream(PipeDirection, HandleInheritability, Int32) |
使用指定的管道方向、可继承性模式和缓冲区大小初始化类的新实例 AnonymousPipeServerStream 。 |
| AnonymousPipeServerStream(PipeDirection, HandleInheritability) |
使用指定的管道方向和可继承性模式初始化类的新实例 AnonymousPipeServerStream 。 |
| AnonymousPipeServerStream(PipeDirection, SafePipeHandle, SafePipeHandle) |
从指定的管道句柄初始化类的新实例 AnonymousPipeServerStream 。 |
| AnonymousPipeServerStream(PipeDirection) |
使用指定的管道方向初始化类的新实例 AnonymousPipeServerStream 。 |
属性
| 名称 | 说明 |
|---|---|
| CanRead |
获取一个值,该值指示当前流是否支持读取操作。 (继承自 PipeStream) |
| CanSeek |
获取一个值,该值指示当前流是否支持查找操作。 (继承自 PipeStream) |
| CanTimeout |
获取一个值,该值确定当前流是否可以超时。 (继承自 Stream) |
| CanWrite |
获取一个值,该值指示当前流是否支持写入操作。 (继承自 PipeStream) |
| ClientSafePipeHandle |
获取当前连接到AnonymousPipeServerStream该对象的对象的安全句柄AnonymousPipeClientStream。 |
| InBufferSize |
获取管道的入站缓冲区的大小(以字节为单位)。 (继承自 PipeStream) |
| IsAsync |
获取一个值,该值指示对象是 PipeStream 异步打开还是同步打开。 (继承自 PipeStream) |
| IsConnected |
获取或设置一个值,该值指示对象是否已 PipeStream 连接。 (继承自 PipeStream) |
| IsHandleExposed |
获取一个值,该值指示是否公开对象的句柄 PipeStream 。 (继承自 PipeStream) |
| IsMessageComplete |
获取一个值,该值指示从最近的读取操作返回的消息中是否有更多数据。 (继承自 PipeStream) |
| Length |
获取流的长度(以字节为单位)。 (继承自 PipeStream) |
| OutBufferSize |
获取管道的出站缓冲区的大小(以字节为单位)。 (继承自 PipeStream) |
| Position |
获取或设置当前流的当前位置。 (继承自 PipeStream) |
| ReadMode |
设置对象的 AnonymousPipeServerStream 读取模式。 对于匿名管道,传输模式必须是 Byte。 |
| ReadTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取的时间。 (继承自 Stream) |
| SafePipeHandle |
获取当前 PipeStream 对象封装的管道的本地端的安全句柄。 (继承自 PipeStream) |
| TransmissionMode |
获取当前管道支持的管道传输模式。 |
| WriteTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时之前尝试写入的时间。 (继承自 Stream) |
方法
扩展方法
| 名称 | 说明 |
|---|---|
| ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可释放项返回的任务的 await。 |