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

定義

開始非同步的讀取作業。Begins an asynchronous read operation. (請考慮用 ReadAsync(Byte[], Int32, Int32, CancellationToken) 替代。)(Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.)

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 count, AsyncCallback callback, object state);
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, count As Integer, callback As AsyncCallback, state As Object) As IAsyncResult

參數

buffer
Byte[]

讀取資料的緩衝區。The buffer to read the data into.

offset
Int32

開始寫入自資料流讀取資料的 buffer 位元組位移。The byte offset in buffer at which to begin writing data read from the stream.

count
Int32

要讀取的最大位元組數。The maximum number of bytes to read.

callback
AsyncCallback

選擇性的非同步回呼,在讀取完成時呼叫。An optional asynchronous callback, to be called when the read is complete.

state
Object

使用者所提供的物件,其可以從其他要求中區分出這個特定非同步讀取的要求。A user-provided object that distinguishes this particular asynchronous read request from other requests.

傳回

物件,表示可能還在暫止中的非同步讀取。An object that represents the asynchronous read, which could still be pending.

例外狀況

buffernullbuffer is null.

offsetcount 為負。offset or count is negative.

嘗試在超過資料流結尾處進行非同步讀取。Attempted an asynchronous read past the end of the stream.

緩衝區長度減去 offset 會小於 countThe buffer length minus offset is less than count.

目前的資料流不支援讀取作業。The current stream does not support the read operation.

備註

在 .NET Framework 4 和舊版中,您必須使用 BeginReadEndRead 等方法來執行非同步檔案作業。In the .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous file operations. 這些方法仍可在 .NET Framework 4.5.NET Framework 4.5 中使用,以支援舊版程式碼;不過,新的非同步方法(例如 ReadAsyncWriteAsyncFlushAsync)可協助您更輕鬆地執行非同步檔案作業。These methods are still available in the .NET Framework 4.5.NET Framework 4.5 to support legacy code; however, the new async methods, such as ReadAsync, WriteAsync, and FlushAsync, help you implement asynchronous file operations more easily.

每次呼叫 BeginRead時,都必須只呼叫一次 EndWriteEndWrite must be called exactly once for every call to BeginRead. 無法在開始另一個讀取之前結束讀取程式,可能會造成不必要的行為,例如鎖死。Failing to end a read process before beginning another read can cause undesirable behavior such as deadlock.

注意

使用 CanRead 屬性來判斷目前的實例是否支援讀取。Use the CanRead property to determine whether the current instance supports reading.

必須使用此 IAsyncResult 呼叫 EndRead,以找出已讀取的位元組數目。EndRead must be called with this IAsyncResult to find out how many bytes were read.

適用於