AnonymousPipeServerStream 类

定义

在匿名管道周围公开流,该管道支持同步和异步读取和写入操作。

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

示例

以下示例使用匿名管道将一个字符串从父进程发送到子进程。 本示例在父进程中创建值为 <a0/> 的对象。它还在子进程中创建值为 <a0/> 的对象。 然后,父进程将用户提供的字符串发送到子进程。 字符串会显示到控制台。

此示例适用于使用 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)

方法

名称 说明
BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

开始异步读取操作。

(继承自 PipeStream)
BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

开始异步写入操作。

(继承自 PipeStream)
CheckPipePropertyOperations()

验证管道是否处于正确的状态以获取或设置属性。

(继承自 PipeStream)
CheckReadOperations()

验证管道是否处于已连接状态进行读取操作。

(继承自 PipeStream)
CheckWriteOperations()

验证管道是否处于写入操作的连接状态。

(继承自 PipeStream)
Close()

关闭当前流并释放与当前流关联的任何资源(如套接字和文件句柄)。 请确保流已正确释放,而不是调用此方法。

(继承自 Stream)
CopyTo(Stream, Int32)

从当前流中读取字节,并使用指定的缓冲区大小将其写入另一个流。 这两个流位置都是按复制的字节数进行高级的。

(继承自 Stream)
CopyTo(Stream)

从当前流中读取字节并将其写入另一个流。 这两个流位置都是按复制的字节数进行高级的。

(继承自 Stream)
CopyToAsync(Stream, CancellationToken)

使用指定的取消标记异步读取当前流中的字节并将其写入另一个流。 这两个流位置都是按复制的字节数进行高级的。

(继承自 Stream)
CopyToAsync(Stream, Int32, CancellationToken)

使用指定的缓冲区大小和取消令牌异步读取当前流中的字节并将其写入另一个流。 这两个流位置都是按复制的字节数进行高级的。

(继承自 Stream)
CopyToAsync(Stream, Int32)

使用指定的缓冲区大小异步读取当前流中的字节并将其写入另一个流。 这两个流位置都是按复制的字节数进行高级的。

(继承自 Stream)
CopyToAsync(Stream)

从当前流异步读取字节并将其写入另一个流。 这两个流位置都是按复制的字节数进行高级的。

(继承自 Stream)
CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
CreateWaitHandle()
已过时.

分配对象 WaitHandle

(继承自 Stream)
Dispose()

释放该 Stream命令使用的所有资源。

(继承自 Stream)
Dispose(Boolean)

释放类使用 PipeStream 的非托管资源,并选择性地释放托管资源。

(继承自 PipeStream)
DisposeAsync()

异步释放由 <a0/a0> 使用的非托管资源。

(继承自 Stream)
DisposeLocalCopyOfClientHandle()

关闭对象的句柄的 AnonymousPipeClientStream 本地副本。

EndRead(IAsyncResult)

结束挂起的异步读取请求。

(继承自 PipeStream)
EndWrite(IAsyncResult)

结束挂起的异步写入请求。

(继承自 PipeStream)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
Finalize()

释放非托管资源,并在垃圾回收实例之前 AnonymousPipeServerStream 执行其他清理操作。

Flush()

清除当前流的缓冲区,并导致任何缓冲数据写入基础设备。

(继承自 PipeStream)
FlushAsync()

异步清除此流的所有缓冲区,并导致任何缓冲数据写入基础设备。

(继承自 Stream)
FlushAsync(CancellationToken)

异步清除此流的所有缓冲区,导致任何缓冲数据写入基础设备,并监视取消请求。

(继承自 Stream)
GetAccessControl()

获取一个 PipeSecurity 对象,该对象封装当前 PipeStream 对象描述的管道的访问控制列表(ACL)条目。

(继承自 PipeStream)
GetClientHandleAsString()

获取连接的 AnonymousPipeClientStream 对象的句柄作为字符串。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 Type

(继承自 Object)
InitializeHandle(SafePipeHandle, Boolean, Boolean)

PipeStream从指定SafePipeHandle对象初始化对象。

(继承自 PipeStream)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
ObjectInvariant()
已过时.

提供对 a Contract.

(继承自 Stream)
Read(Byte[], Int32, Int32)

从流中读取字节块,并将数据写入指定缓冲区,从指定长度的指定位置开始。

(继承自 PipeStream)
Read(Span<Byte>)

从当前流中读取字节序列,将其写入字节数组,并通过读取的字节数推进流中的位置。

(继承自 PipeStream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

将当前流中的字节序列异步读取到从指定字节数开始的字节数组,按读取的字节数推进流中的位置,并监视取消请求。

(继承自 PipeStream)
ReadAsync(Byte[], Int32, Int32)

从当前流异步读取字节序列,并通过读取的字节数推进流中的位置。

(继承自 Stream)
ReadAsync(Memory<Byte>, CancellationToken)

从当前流异步读取字节序列,将其写入字节内存范围,按读取的字节数推进流中的位置,并监视取消请求。

(继承自 PipeStream)
ReadByte()

从管道读取字节。

(继承自 PipeStream)
Seek(Int64, SeekOrigin)

将当前流的当前位置设置为指定值。

(继承自 PipeStream)
SetAccessControl(PipeSecurity)

将对象指定的 PipeSecurity 访问控制列表(ACL)条目应用于当前 PipeStream 对象指定的管道。

(继承自 PipeStream)
SetLength(Int64)

将当前流的长度设置为指定值。

(继承自 PipeStream)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
WaitForPipeDrain()

等待管道的另一端读取所有发送的字节。

(继承自 PipeStream)
Write(Byte[], Int32, Int32)

使用缓冲区中的数据将字节块写入当前流。

(继承自 PipeStream)
Write(ReadOnlySpan<Byte>)

将字节序列写入当前流,并通过写入的字节数推进此流中的当前位置。

(继承自 PipeStream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

从从指定位置开始的字节数组异步写入指定的字节数,按写入的字节数推进此流中的当前位置,并监视取消请求。

(继承自 PipeStream)
WriteAsync(Byte[], Int32, Int32)

以异步方式将字节序列写入当前流,并通过写入的字节数推进此流中的当前位置。

(继承自 Stream)
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

将字节序列异步写入当前流,按写入的字节数推进此流中的当前位置,并监视取消请求。

(继承自 PipeStream)
WriteByte(Byte)

将字节写入当前流。

(继承自 PipeStream)

扩展方法

名称 说明
ConfigureAwait(IAsyncDisposable, Boolean)

配置如何执行从异步可释放项返回的任务的 await。

适用于