NetworkStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 方法

定義

開始從 NetworkStream 進行非同步讀取。

public:
 override IAsyncResult ^ BeginRead(cli::array <System::Byte> ^ buffer, int offset, int size, AsyncCallback ^ callback, System::Object ^ state);
public:
 override IAsyncResult ^ BeginRead(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int size, AsyncCallback? callback, object? state);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback? callback, object? state);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int size, AsyncCallback callback, object state);
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, size As Integer, callback As AsyncCallback, state As Object) As IAsyncResult
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, count As Integer, callback As AsyncCallback, state As Object) As IAsyncResult

參數

buffer
Byte[]

Byte 型別的陣列,這是存放讀取自 NetworkStream 的資料的記憶體位置。

offset
Int32

buffer 中的位置,要在此處開始存放資料。

sizecount
Int32

要從 NetworkStream 讀取的位元組數。

callback
AsyncCallback

AsyncCallback 委派,會在 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 完成時加以執行。

state
Object

物件,包含任何額外的使用者定義資料。

傳回

IAsyncResult,表示非同步呼叫。

例外狀況

buffer 參數為 null

offset 參數小於 0。

-或-

offset 參數大於 buffer 參數的長度。

-或-

size 小於 0。

-或-

size 大於 buffer 的長度減去 offset 參數值。

基礎 Socket 已經關閉。

-或-

從網路讀取時發生失敗。

-或-

存取通訊端時發生錯誤。

備註

重要

這是相容性 API,不建議使用 APM (Begin / End) 方法進行新的開發。 請改用以工作為基礎的對等專案。

您可以傳遞實作 AsyncCallbackBeginRead 的回呼,以取得作業完成的通知。 請注意,如果基礎網路堆疊以同步方式完成作業,則會在呼叫 BeginRead期間內嵌執行回呼。 在此情況下, CompletedSynchronously 傳回 IAsyncResult 的 屬性會設定為 true ,以指出方法已同步完成。 AsyncState使用 的 IAsyncResult 屬性,取得傳遞至 BeginRead 方法的狀態物件。

呼叫 BeginRead 方法必須完成 EndRead 作業。 一般而言,提供的 AsyncCallback 委派會叫用 方法。 EndRead 將會封鎖呼叫線程,直到作業完成為止。

作業會讀取可用的數據量,最多為 參數所 size 指定的位元元組數目。

注意

如果您收到 IOException,請檢查 InnerException 屬性,以判斷它是否由 SocketException所造成。 如果是,請使用 ErrorCode 屬性來取得特定的錯誤碼。

讀取和寫入作業可以在類別的 NetworkStream 實例上同時執行,而不需要同步處理。 只要寫入作業有一個唯一線程,且讀取作業有一個唯一線程,讀取和寫入線程之間就不會有交叉干擾,而且不需要同步處理。

適用於

另請參閱