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 一個物件,其 PipeDirection 值為 PipeDirection.Out。它也會在子程序中建立 AnonymousPipeClientStream 一個物件,其 PipeDirection 值為 PipeDirection.In。 父程序接著將使用者提供的字串傳送給子程序。 這串字串會顯示在主控台上。
這個範例是針對伺服器程序,該程序使用 了 這個 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方法丟棄客戶端句柄,才能在客戶端退出時收到通知。
建構函式
| 名稱 | Description |
|---|---|
| AnonymousPipeServerStream() |
初始化 AnonymousPipeServerStream 類別的新執行個體。 |
| AnonymousPipeServerStream(PipeDirection, HandleInheritability, Int32, PipeSecurity) |
初始化類別的新實例 AnonymousPipeServerStream ,並依指定的管線方向、可繼承模式、緩衝區大小及管線安全性。 |
| AnonymousPipeServerStream(PipeDirection, HandleInheritability, Int32) |
初始化一個新的類別實例 AnonymousPipeServerStream ,並依指定的管線方向、可繼承模式及緩衝區大小。 |
| AnonymousPipeServerStream(PipeDirection, HandleInheritability) |
初始化一個新的類別實例 AnonymousPipeServerStream ,使用指定的管線方向與可繼承模式。 |
| AnonymousPipeServerStream(PipeDirection, SafePipeHandle, SafePipeHandle) |
從指定的管線 handle 初始化該 AnonymousPipeServerStream 類別的新實例。 |
| AnonymousPipeServerStream(PipeDirection) |
初始化一個以指定管線方向的新類別實例 AnonymousPipeServerStream 。 |
屬性
| 名稱 | Description |
|---|---|
| CanRead |
會取得一個值,表示目前串流是否支援讀取操作。 (繼承來源 PipeStream) |
| CanSeek |
會獲得一個值,表示目前串流是否支援尋道操作。 (繼承來源 PipeStream) |
| CanTimeout |
取得值,這個值會判斷目前的數據流是否可以逾時。 (繼承來源 Stream) |
| CanWrite |
會獲得一個值,表示目前串流是否支援寫入操作。 (繼承來源 PipeStream) |
| ClientSafePipeHandle |
取得目前連接物件AnonymousPipeClientStream的安全柄AnonymousPipeServerStream。 |
| InBufferSize |
取得管線入站緩衝區的大小(以位元組為單位)。 (繼承來源 PipeStream) |
| IsAsync |
會取得一個值,表示物件是 PipeStream 非同步或同步開啟。 (繼承來源 PipeStream) |
| IsConnected |
取得或設定一個值,表示物件是否 PipeStream 連通。 (繼承來源 PipeStream) |
| IsHandleExposed |
會取得一個值,表示物件的 handle PipeStream 是否被暴露。 (繼承來源 PipeStream) |
| IsMessageComplete |
會取得一個值,表示最近一次讀取操作回傳的訊息中是否有更多資料。 (繼承來源 PipeStream) |
| Length |
取得串流長度,以位元組為單位。 (繼承來源 PipeStream) |
| OutBufferSize |
取得管線出站緩衝區的大小(以位元組為單位)。 (繼承來源 PipeStream) |
| Position |
取得或設定當前串流的當前位置。 (繼承來源 PipeStream) |
| ReadMode |
設定物件的 AnonymousPipeServerStream 讀取模式。 對於匿名管道,傳輸模式必須為 Byte。 |
| ReadTimeout |
會取得或設定一個以毫秒為單位的值,決定串流在逾時前嘗試讀取的時間長短。 (繼承來源 Stream) |
| SafePipeHandle |
會拿到管子本地端的安全把手,該管子是當前 PipeStream 物體所包覆的。 (繼承來源 PipeStream) |
| TransmissionMode |
取得由當前管線支援的管道傳輸模式。 |
| WriteTimeout |
取得或設定一個毫秒級的值,決定串流在逾時前嘗試寫入的時間長短。 (繼承來源 Stream) |
方法
| 名稱 | Description |
|---|---|
| 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() |
非同步釋放 Stream. (繼承來源 Stream) |
| DisposeLocalCopyOfClientHandle() |
關閉物件 handle 的 AnonymousPipeClientStream 本地副本。 |
| EndRead(IAsyncResult) |
結束暫止的異步讀取要求。 (繼承來源 PipeStream) |
| EndWrite(IAsyncResult) |
結束一個待處理的非同步寫入請求。 (繼承來源 PipeStream) |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| Finalize() |
在實例被垃圾回收前,釋放未管理的資源並執行其他清理操作 AnonymousPipeServerStream 。 |
| Flush() |
清除當前串流的緩衝區,並使任何緩衝資料寫入底層裝置。 (繼承來源 PipeStream) |
| FlushAsync() |
非同步清除此串流的所有緩衝區,並使任何緩衝資料寫入底層裝置。 (繼承來源 Stream) |
| FlushAsync(CancellationToken) |
非同步清除當前串流的緩衝區,並使任何緩衝資料寫入底層裝置。 (繼承來源 PipeStream) |
| GetAccessControl() |
取得 PipeSecurity 一個物件,封裝了目前物件所描述 PipeStream 管線的存取控制清單(ACL)條目。 (繼承來源 PipeStream) |
| GetClientHandleAsString() |
會以字串的形式取得連接 AnonymousPipeClientStream 物件的 handle。 |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetLifetimeService() |
已淘汰.
擷取控制這個實例存留期原則的目前存留期服務物件。 (繼承來源 MarshalByRefObject) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| InitializeHandle(SafePipeHandle, Boolean, Boolean) |
從指定的PipeStream物件初始化一個SafePipeHandle物件。 (繼承來源 PipeStream) |
| InitializeLifetimeService() |
已淘汰.
取得存留期服務物件,以控制這個實例的存留期原則。 (繼承來源 MarshalByRefObject) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| MemberwiseClone(Boolean) |
建立目前 MarshalByRefObject 對象的淺層複本。 (繼承來源 MarshalByRefObject) |
| ObjectInvariant() |
已淘汰.
提供 。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) |
| ReadAtLeast(Span<Byte>, Int32, Boolean) |
從目前數據流讀取至少一個字節數目,並將數據流中的位置依讀取的位元組數目往前移。 (繼承來源 Stream) |
| ReadAtLeastAsync(Memory<Byte>, Int32, Boolean, CancellationToken) |
以異步方式從目前數據流讀取至少一個字節數目、依讀取的位元組數目將數據流中的位置往前移,並監視取消要求。 (繼承來源 Stream) |
| ReadByte() |
讀取一個位元組。 (繼承來源 PipeStream) |
| ReadExactly(Byte[], Int32, Int32) |
從目前串流讀取 |
| ReadExactly(Span<Byte>) |
從目前串流讀取位元組,並推進串流中的位置直到填 |
| ReadExactlyAsync(Byte[], Int32, Int32, CancellationToken) |
非同步讀取 |
| ReadExactlyAsync(Memory<Byte>, CancellationToken) |
非同步讀取目前串流的位元組,推進串流中的位置直到填 |
| 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) |
擴充方法
| 名稱 | Description |
|---|---|
| ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從異步可處置專案傳回的工作等候。 |
| CopyToAsync(Stream, PipeWriter, CancellationToken) |
非同步讀取 的 Stream 位元組並使用消去標記寫入指定的 PipeWriter。 |
| GetAccessControl(PipeStream) |
回傳管線流的安全資訊。 |
| SetAccessControl(PipeStream, PipeSecurity) |
變更現有管線串流的安全屬性。 |