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以支持旧版代码; 但是,新的异步方法,如ReadAsyncWriteAsyncCopyToAsync,和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 必须为每个调用一次调用BeginWriteEndWrite must be called once for every call to BeginWrite. 您可以执行此操作是通过调用的相同代码BeginWrite或在传递给回调BeginWriteYou 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.

如果流已关闭或传递了无效的参数,将立即引发异常从BeginWriteIf a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginWrite. 异步写入请求,例如,I/O 请求过程的磁盘故障过程中发生的错误发生在线程池线程和调用时引发异常EndWriteErrors 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.

适用于

另请参阅