AnonymousPipeClientStream 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
公开匿名管道流的客户端,该流支持同步和异步读取和写入操作(Windows 平台上不支持取消)。
public ref class AnonymousPipeClientStream sealed : System::IO::Pipes::PipeStream
public sealed class AnonymousPipeClientStream : System.IO.Pipes.PipeStream
type AnonymousPipeClientStream = class
inherit PipeStream
Public NotInheritable Class AnonymousPipeClientStream
Inherits PipeStream
- 继承
示例
以下示例使用匿名管道将父进程中的字符串发送到子进程。 本示例在父进程中创建
此示例适用于由服务器进程启动的客户端进程。 为客户端代码 pipeClient.exe 生成的可执行文件命名,并在运行此示例时将其复制到服务器可执行文件所在的同一目录中。 有关整个代码示例(包括管道客户端和服务器的代码)请参阅 如何:使用匿名管道进行本地进程间通信。
//<snippet01>
using System;
using System.IO;
using System.IO.Pipes;
class PipeClient
{
static void Main(string[] args)
{
if (args.Length > 0)
{
using (PipeStream pipeClient =
new AnonymousPipeClientStream(PipeDirection.In, args[0]))
{
Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.",
pipeClient.TransmissionMode);
using (StreamReader sr = new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
// Wait for 'sync message' from the server.
do
{
Console.WriteLine("[CLIENT] Wait for sync...");
temp = sr.ReadLine();
}
while (!temp.StartsWith("SYNC"));
// Read the server data and echo to the console.
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("[CLIENT] Echo: " + temp);
}
}
}
}
Console.Write("[CLIENT] Press Enter to continue...");
Console.ReadLine();
}
}
//</snippet01>
'<snippet01>
Imports System.IO
Imports System.IO.Pipes
Class PipeClient
Shared Sub Main(args() as String)
If args.Length > 0 Then
Using pipeClient As New AnonymousPipeClientStream(PipeDirection.In, args(0))
Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.", _
pipeClient.TransmissionMode)
Using sr As New StreamReader(pipeClient)
' Display the read text to the console
Dim temp As String
' Wait for 'sync message' from the server.
Do
Console.WriteLine("[CLIENT] Wait for sync...")
temp = sr.ReadLine()
Loop While temp.StartsWith("SYNC") = False
' Read the server data and echo to the console.
temp = sr.ReadLine()
While Not temp = Nothing
Console.WriteLine("[CLIENT] Echo: " + temp)
temp = sr.ReadLine()
End While
End Using
End Using
End If
Console.Write("[CLIENT] Press Enter to continue...")
Console.ReadLine()
End Sub
End Class
'</snippet01>
注解
匿名管道有助于在子进程和父进程之间提供安全可靠的进程间通信。 该 AnonymousPipeClientStream 类使子进程能够连接到父进程并与父进程交换信息。
匿名管道是未命名的单向管道,通常在父进程和子进程之间传输数据。 匿名管道始终是本地管道;不能通过网络使用它们。 PipeDirection不支持值InOut,因为匿名管道定义为单向管道。
匿名管道不支持 PipeTransmissionMode.Message 读取模式。
匿名管道的客户端必须通过调用 GetClientHandleAsString 该方法从服务器端提供的管道句柄创建。 然后,在创建客户端进程时将字符串作为参数传递。 在客户端进程中,它将作为pipeHandleAsString参数传递给AnonymousPipeClientStream构造函数。
在Windows,匿名管道不支持异步(重叠)读取和写入操作(请参阅 Anonymous 管道操作)。 AnonymousPipeClientStream 类仍将在Windows平台上的线程池上安排工作,因此异步Stream操作工作,但不支持取消这些操作。
构造函数
| 名称 | 说明 |
|---|---|
| AnonymousPipeClientStream(PipeDirection, SafePipeHandle) |
从指定的句柄初始化类的新实例 AnonymousPipeClientStream 。 |
| AnonymousPipeClientStream(PipeDirection, String) |
使用指定的管道方向和管道句柄的字符串表示形式初始化类的新实例 AnonymousPipeClientStream 。 |
| AnonymousPipeClientStream(String) |
使用管道句柄的指定字符串表示形式初始化类的新实例 AnonymousPipeClientStream 。 |
属性
| 名称 | 说明 |
|---|---|
| CanRead |
获取一个值,该值指示当前流是否支持读取操作。 (继承自 PipeStream) |
| CanSeek |
获取一个值,该值指示当前流是否支持查找操作。 (继承自 PipeStream) |
| CanTimeout |
获取一个值,该值确定当前流是否可以超时。 (继承自 Stream) |
| CanWrite |
获取一个值,该值指示当前流是否支持写入操作。 (继承自 PipeStream) |
| InBufferSize |
获取管道的入站缓冲区的大小(以字节为单位)。 (继承自 PipeStream) |
| IsAsync |
获取一个值,该值指示对象是 PipeStream 异步打开还是同步打开。 (继承自 PipeStream) |
| IsConnected |
获取或设置一个值,该值指示对象是否已 PipeStream 连接。 (继承自 PipeStream) |
| IsHandleExposed |
获取一个值,该值指示是否公开对象的句柄 PipeStream 。 (继承自 PipeStream) |
| IsMessageComplete |
获取一个值,该值指示从最近的读取操作返回的消息中是否有更多数据。 (继承自 PipeStream) |
| Length |
获取流的长度(以字节为单位)。 (继承自 PipeStream) |
| OutBufferSize |
获取管道的出站缓冲区的大小(以字节为单位)。 (继承自 PipeStream) |
| Position |
获取或设置当前流的当前位置。 (继承自 PipeStream) |
| ReadMode |
设置对象的 AnonymousPipeClientStream 读取模式。 |
| ReadTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取的时间。 (继承自 Stream) |
| SafePipeHandle |
获取当前 PipeStream 对象封装的管道的本地端的安全句柄。 (继承自 PipeStream) |
| TransmissionMode |
获取当前管道支持的管道传输模式。 |
| WriteTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时之前尝试写入的时间。 (继承自 Stream) |
方法
扩展方法
| 名称 | 说明 |
|---|---|
| ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可释放项返回的任务的 await。 |