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 類別的伺服器進程。 如需整個程式代碼範例,包括管道用戶端和伺服器的程式代碼,請參閱 How to: Use Anonymous Pipes for Local Interprocess Communication。
//<snippet01>
#using <System.dll>
#using <System.Core.dll>
using namespace System;
using namespace System::IO;
using namespace System::IO::Pipes;
using namespace System::Diagnostics;
ref class PipeServer
{
public:
static void Main()
{
Process^ pipeClient = gcnew Process();
pipeClient->StartInfo->FileName = "pipeClient.exe";
AnonymousPipeServerStream^ pipeServer =
gcnew 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.
StreamWriter^ sw = gcnew 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());
sw->Close();
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException^ e)
{
Console::WriteLine("[SERVER] Error: {0}", e->Message);
}
pipeServer->Close();
pipeClient->WaitForExit();
pipeClient->Close();
Console::WriteLine("[SERVER] Client quit. Server terminating.");
}
};
int main()
{
PipeServer::Main();
}
//</snippet01>
//<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) |
使用指定的管道方向,初始化 AnonymousPipeServerStream 類別的新執行個體。 |
AnonymousPipeServerStream(PipeDirection, HandleInheritability) |
使用指定的管道方向與繼承模式,初始化 AnonymousPipeServerStream 類別的新執行個體。 |
AnonymousPipeServerStream(PipeDirection, HandleInheritability, Int32) |
使用指定的管道方向、繼承模式及緩衝區大小,初始化 AnonymousPipeServerStream 類別的新執行個體。 |
AnonymousPipeServerStream(PipeDirection, HandleInheritability, Int32, PipeSecurity) |
使用指定的管道方向、繼承模式、緩衝區大小及管道安全性,初始化 AnonymousPipeServerStream 類別的新執行個體。 |
AnonymousPipeServerStream(PipeDirection, SafePipeHandle, SafePipeHandle) |
從指定的管道控制碼初始化 AnonymousPipeServerStream 類別的新執行個體。 |
屬性
CanRead |
取得值,表示目前的資料流是否支援讀取作業。 (繼承來源 PipeStream) |
CanSeek |
取得值,表示目前的資料流是否支援搜尋作業。 (繼承來源 PipeStream) |
CanTimeout |
取得值,該值判斷目前的資料流是否可以逾時。 (繼承來源 Stream) |
CanWrite |
取得值,表示目前的資料流是否支援寫入作業。 (繼承來源 PipeStream) |
ClientSafePipeHandle |
取得 AnonymousPipeClientStream 物件的安全控制碼,此物件目前連接到 AnonymousPipeServerStream 物件。 |
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) |
從目前資料流讀取位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。 (繼承來源 Stream) |
CopyTo(Stream, Int32) |
使用指定的緩衝區大小,從目前資料流讀取所有位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。 (繼承來源 Stream) |
CopyToAsync(Stream) |
以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。 (繼承來源 Stream) |
CopyToAsync(Stream, CancellationToken) |
使用指定的取消權杖,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。 這兩個數據流位置都是由複製的位元元組數目進階。 (繼承來源 Stream) |
CopyToAsync(Stream, Int32) |
使用指定的緩衝區大小,以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。 (繼承來源 Stream) |
CopyToAsync(Stream, Int32, CancellationToken) |
使用指定的緩衝區大小和取消語彙基元,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。 這兩個數據流位置都是由複製的位元元組數目進階。 (繼承來源 Stream) |
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。 (繼承來源 MarshalByRefObject) |
CreateWaitHandle() |
已淘汰.
已淘汰.
已淘汰.
配置 WaitHandle 物件。 (繼承來源 Stream) |
Dispose() |
釋放 Stream 所使用的所有資源。 (繼承來源 Stream) |
Dispose(Boolean) |
釋放 PipeStream 類別所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。 (繼承來源 PipeStream) |
DisposeAsync() |
以非同步方式釋放 Stream 使用的不受控資源。 (繼承來源 Stream) |
DisposeLocalCopyOfClientHandle() |
關閉 AnonymousPipeClientStream 物件控制碼的本機複本。 |
EndRead(IAsyncResult) |
結束擱置的非同步讀取要求。 (繼承來源 PipeStream) |
EndWrite(IAsyncResult) |
結束暫止的非同步寫入要求。 (繼承來源 PipeStream) |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Finalize() |
釋出 Unmanaged 資源並執行其他清除作業後,記憶體回收才能重新回收 AnonymousPipeServerStream 執行個體。 |
Flush() |
清除目前資料流的緩衝區,並讓所有的緩衝資料全部寫入基礎裝置。 (繼承來源 PipeStream) |
FlushAsync() |
以非同步的方式清除這個資料流的所有緩衝區,並造成所有緩衝資料都寫入基礎裝置。 (繼承來源 Stream) |
FlushAsync(CancellationToken) |
以非同步方式清除目前資料流的緩衝區,並讓所有的緩衝資料全部寫入底層裝置。 (繼承來源 PipeStream) |
GetAccessControl() |
取得 PipeSecurity 物件,該物件會封裝目前 PipeStream 物件所描述之管道的存取控制清單 (ACL) 項目。 (繼承來源 PipeStream) |
GetClientHandleAsString() |
取得已連接 AnonymousPipeClientStream 物件的控制碼,做為字串。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetLifetimeService() |
已淘汰.
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。 (繼承來源 MarshalByRefObject) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
InitializeHandle(SafePipeHandle, Boolean, Boolean) |
從指定的 SafePipeHandle 物件初始化 PipeStream 物件。 (繼承來源 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) |
以非同步的方式從目前的資料流讀取位元組序列,並依讀取的位元組數將資料流中的位置往前移。 (繼承來源 Stream) |
ReadAsync(Byte[], Int32, Int32, CancellationToken) |
以非同步方式將目前資料流中位元組序列讀入起始於指定位置且具有指定位元組數的位元組陣列、依讀取的位元組數將資料流中位置往前移,並監視取消要求。 (繼承來源 PipeStream) |
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) |
以非同步的方式將位元組序列寫入至目前的資料流,並依寫入的位元組數將資料流中目前的位置往前移。 (繼承來源 Stream) |
WriteAsync(Byte[], Int32, Int32, CancellationToken) |
以非同步方式從起始於指定位置的位元組陣列寫入所指定位元組數、依寫入的位元組數將此資料流中目前位置往前移,並監視取消要求。 (繼承來源 PipeStream) |
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) |
以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。 (繼承來源 PipeStream) |
WriteByte(Byte) |
將位元組寫入目前的資料流。 (繼承來源 PipeStream) |
擴充方法
CopyToAsync(Stream, PipeWriter, CancellationToken) |
使用取消語彙基元,以非同步的方式從 Stream 讀取位元組,並將其寫入指定的 PipeWriter。 |
GetAccessControl(PipeStream) |
傳回管道串流的安全性資訊。 |
SetAccessControl(PipeStream, PipeSecurity) |
變更現有管道串流的安全性屬性。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應