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

定義

開始非同步的寫入作業。Begins an asynchronous write operation. (請考慮用 WriteAsync(Byte[], Int32, Int32) 替代。)(Consider using WriteAsync(Byte[], Int32, Int32) instead.)

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

參數

buffer
Byte[]

寫入資料的來源緩衝區。The buffer to write data from.

offset
Int32 Int32 Int32 Int32

buffer 中要開始寫入之處的位元組位移。The byte offset in buffer from which to begin writing.

count
Int32 Int32 Int32 Int32

寫入的最大位元組數。The maximum number of bytes to write.

callback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

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

state
Object Object Object Object

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

傳回

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

例外狀況

嘗試超出資料流結尾的非同步寫入,或發生磁碟錯誤。Attempted an asynchronous write 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 write operation.

備註

在 .NET Framework 4 (含) 以前版本中,您必須使用方法 (例如 BeginWriteEndWrite ) 實作非同步 I/O 作業。In the .NET Framework 4 and earlier versions, you have to use methods such as BeginWrite and EndWrite 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.

資料流程BeginWrite上的預設執行Write會以同步Write方式呼叫方法,這表示可能會在某些資料流程上封鎖。The default implementation of BeginWrite on a stream calls the Write method synchronously, which means that Write 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. 因此,對BeginWrite的呼叫不會在這些資料流程上封鎖。Therefore, calls to BeginWrite will not block on those streams. 您可以覆BeginWrite寫(例如,藉由使用非同步委派)來提供非同步行為。You can override BeginWrite (by using async delegates, for example) to provide asynchronous behavior.

將目前IAsyncResult方法所傳回的傳遞至EndWrite ,以確保寫入會完成,並適當地釋放資源。Pass the IAsyncResult returned by the current method to EndWrite to ensure that the write completes and frees resources appropriately. EndWrite每次呼叫BeginWrite時, 都必須呼叫一次。EndWrite must be called once for every call to BeginWrite. 您可以在傳遞至BeginWrite BeginWrite的回呼中,使用呼叫或的相同程式碼來執行這項操作。You can do this either by using the same code that called BeginWrite or in a callback passed to BeginWrite. 如果在非同步寫入期間發生錯誤,將不會擲回例外狀況, EndWrite直到使用這個方法IAsyncResult所傳回的呼叫為止。If an error occurs during an asynchronous write, an exception will not be thrown until EndWrite is called with the IAsyncResult returned by this method.

如果資料流程可寫入,則在資料流程結尾寫入會展開資料流程。If a stream is writable, writing at the end of the stream expands the stream.

當您發出非同步讀取或寫入,而不是在 i/o 作業完成時,就會更新資料流程中的目前位置。The current position in the stream is updated when you issue the asynchronous read or write, not when the I/O operation completes. 多個同時非同步要求會導致要求完成順序不確定。Multiple simultaneous asynchronous requests render the request completion order uncertain.

CanWrite使用屬性來判斷目前的實例是否支援寫入。Use the CanWrite property to determine whether the current instance supports writing.

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

適用於

另請參閱