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
- 繼承
- 繼承
範例
下列範例會使用匿名管道,將字串從父進程傳送至子進程。 這個範例會在 AnonymousPipeServerStream 父進程中 PipeDirection 建立 值為 Out的物件。它也會在子進程中建立 AnonymousPipeClientStream 物件,其 PipeDirection 值為 In。 接著,父處理序會將使用者提供的字串傳送給子處理序。 字串會顯示在控制台中。
此範例適用於由伺服器進程啟動的客戶端進程。 將產生的可執行檔命名為用戶端程式代碼 pipeClient.exe
,並在執行此範例時,將其複製到與伺服器可執行檔相同的目錄。 如需整個程式代碼範例,包括管道用戶端和伺服器的程式代碼,請參閱 如何:使用匿名管道進行本機進程間通訊。
//<snippet01>
#using <System.Core.dll>
using namespace System;
using namespace System::IO;
using namespace System::IO::Pipes;
ref class PipeClient
{
public:
static void Main(array<String^>^ args)
{
if (args->Length > 1)
{
PipeStream^ pipeClient = gcnew AnonymousPipeClientStream(PipeDirection::In, args[1]);
Console::WriteLine("[CLIENT] Current TransmissionMode: {0}.",
pipeClient->TransmissionMode);
StreamReader^ sr = gcnew 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()) != nullptr)
{
Console::WriteLine("[CLIENT] Echo: " + temp);
}
sr->Close();
pipeClient->Close();
}
Console::Write("[CLIENT] Press Enter to continue...");
Console::ReadLine();
}
};
int main()
{
array<String^>^ args = Environment::GetCommandLineArgs();
PipeClient::Main(args);
}
//</snippet01>
//<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 方法,從伺服器端提供的管道句柄建立。 然後,建立客戶端進程時,字串會當做參數傳遞。 從客戶端進程,它會以 參數的形式傳遞至 AnonymousPipeClientStream 建構函 pipeHandleAsString
式。
在 Windows 上,匿名管道不支援異步 (重疊) 讀取和寫入作業, (請參閱 匿名管道作業) 。 類別 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) |
方法
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) |
EndRead(IAsyncResult) |
結束擱置的非同步讀取要求。 (繼承來源 PipeStream) |
EndWrite(IAsyncResult) |
結束暫止的非同步寫入要求。 (繼承來源 PipeStream) |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Finalize() |
釋出 Unmanaged 資源並執行其他清除作業後,記憶體回收才能重新回收 AnonymousPipeClientStream 執行個體。 |
Flush() |
清除目前資料流的緩衝區,並讓所有的緩衝資料全部寫入基礎裝置。 (繼承來源 PipeStream) |
FlushAsync() |
以非同步的方式清除這個資料流的所有緩衝區,並造成所有緩衝資料都寫入基礎裝置。 (繼承來源 Stream) |
FlushAsync(CancellationToken) |
以非同步方式清除目前資料流的緩衝區,並讓所有的緩衝資料全部寫入底層裝置。 (繼承來源 PipeStream) |
GetAccessControl() |
取得 PipeSecurity 物件,該物件會封裝目前 PipeStream 物件所描述之管道的存取控制清單 (ACL) 項目。 (繼承來源 PipeStream) |
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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應