System.IO.FileStream.BeginWrite Method

Begins an asynchronous write operation. (Consider using FileStream.WriteAsync(Byte[], int, int, System.Threading.CancellationToken) instead; see the Remarks section.)

Syntax

public override IAsyncResult BeginWrite (byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)

Parameters

array
The buffer containing data to write to the current stream.
offset
The zero-based byte offset in array at which to begin copying bytes to the current stream.
numBytes
The maximum number of bytes to write.
userCallback
The method to be called when the asynchronous write operation is completed.
stateObject
A user-provided object that distinguishes this particular asynchronous write request from other requests.

Returns

An object that references the asynchronous write.

Exceptions

TypeReason
ArgumentExceptionThe sum of offset and numBytes is greater than the length of array.
ArgumentNullException array is null.
ArgumentOutOfRangeException offset or numBytes is negative.
System.IO.IOExceptionAn I/O error occurred.
SystemNotSupportedExceptionThe stream does not support writing.

Remarks

In the .NET Framework 4 and earlier versions, you have to use methods such as FileStream.BeginWrite(Byte[], int, int, AsyncCallback, object) and FileStream.EndWrite(IAsyncResult) to implement asynchronous file operations. These methods are still available in the net_v45 to support legacy code; however, the new async methods, such as FileStream.ReadAsync(Byte[], int, int, System.Threading.CancellationToken), FileStream.WriteAsync(Byte[], int, int, System.Threading.CancellationToken), Stream.CopyToAsync(Stream), and FileStream.FlushAsync(System.Threading.CancellationToken), help you implement asynchronous file operations more easily.

FileStream.EndWrite(IAsyncResult) must be called exactly once on every IAsyncResult from FileStream.BeginWrite(Byte[], int, int, AsyncCallback, object). FileStream.EndWrite(IAsyncResult) will block until the I/O operation has completed.

This method overrides Stream.BeginWrite(Byte[], int, int, AsyncCallback, object).

System.IO.FileStream provides two different modes of operation: synchronous I/O and asynchronous I/O. While either can be used, the underlying operating system resources might allow access in only one of these modes. By default, System.IO.FileStream opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the FileStream.#ctor(string, FileMode, FileAccess, FileShare, int, bool) constructor.

If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from FileStream.BeginWrite(Byte[], int, int, AsyncCallback, object). Errors that occur during an asynchronous write request, such as a disk failure during the IO request, occur on the thread pool thread and become visible upon a call to FileStream.EndWrite(IAsyncResult).

Multiple simultaneous asynchronous requests render the request completion order uncertain.

For a list of common file and directory operations, see Common I/O Tasks.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0