Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Method

定義

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

public:
 virtual IAsyncResult ^ BeginRead(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ callback, System::Object ^ state);
public virtual IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback callback, object state);
abstract member BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overridable 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 Int32 Int32 Int32

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

count
Int32 Int32 Int32 Int32

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

callback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

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

state
Object Object Object Object

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

傳回

IAsyncResult,代表可能還在擱置中的非同步讀取。An IAsyncResult that represents the asynchronous read, which could still be pending.

例外狀況

嘗試超越資料流結尾的非同步讀取,或發生磁碟錯誤。Attempted an asynchronous read past the end of the stream, or a disk error occurs.

一或多個引數無效。One or more of the arguments is invalid.

關閉資料流後呼叫了方法。Methods were called after the stream was closed.

目前的 Stream 實作不支援讀取作業。The current Stream implementation does not support the read operation.

備註

在 .NET Framework 4 (含) 以前版本中,您必須使用方法 (例如 BeginReadEndRead ) 實作非同步 I/O 作業。In the .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous I/O operations. 這些方法仍.NET Framework 4.5.NET Framework 4.5可在中使用,以支援舊版程式碼; 不過,新的非同步方法( ReadAsync例如、 CopyToAsync WriteAsync、和FlushAsync)可以協助您更輕鬆地執行非同步 i/o 作業。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, CopyToAsync, and FlushAsync, help you implement asynchronous I/O operations more easily.

資料流程BeginRead上的預設執行Read會以同步Read方式呼叫方法,這表示可能會在某些資料流程上封鎖。The default implementation of BeginRead on a stream calls the Read method synchronously, which means that Read might block on some streams. 不過,如FileStreamNetworkStream等類別的實例,會在實例以非同步方式開啟時,完整支援非同步作業。However, instances of classes such as FileStream and NetworkStream fully support asynchronous operations if the instances have been opened asynchronously. 因此,對BeginRead的呼叫不會在這些資料流程上封鎖。Therefore, calls to BeginRead will not block on those streams. 您可以覆BeginRead寫(例如,藉由使用非同步委派)來提供非同步行為。You can override BeginRead (by using async delegates, for example) to provide asynchronous behavior.

將傳回值傳遞給資料流程EndRead的方法,以判斷已讀取的位元組數目,以及釋放用於讀取的作業系統資源。 IAsyncResultPass the IAsyncResult return value to the EndRead method of the stream to determine how many bytes were read and to release operating system resources used for reading. EndRead每次呼叫BeginRead時, 都必須呼叫一次。EndRead must be called once for every call to BeginRead. 您可以在傳遞至BeginRead BeginRead的回呼中,使用呼叫或的相同程式碼來執行這項操作。You can do this either by using the same code that called BeginRead or in a callback passed to BeginRead.

發出非同步讀取或寫入時,資料流程中的目前位置會更新,而非在 i/o 作業完成時。The current position in the stream is updated when the asynchronous read or write is issued, not when the I/O operation completes.

多個同時非同步要求會導致要求完成順序不確定。Multiple simultaneous asynchronous requests render the request completion order uncertain.

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

如果資料流程已關閉,或您傳遞了不正確引數,則會立即BeginRead從擲回例外狀況。If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginRead. 非同步讀取要求期間發生的錯誤(例如 i/o 要求期間的磁片失敗)會發生線上程集區執行緒上,並在呼叫EndRead時擲回例外狀況。Errors that occur during an asynchronous read request, such as a disk failure during the I/O request, occur on the thread pool thread and throw exceptions when calling EndRead.

適用於

另請參閱