FileStream.BeginWrite Method (Byte(), Int32, Int32, AsyncCallback, Object)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Begins an asynchronous write operation. (Consider using WriteAsync instead; see the Remarks section.)

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading := True)>
Public Overrides Function BeginWrite (
	array As Byte(),
	offset As Integer,
	numBytes As Integer,
	userCallback As AsyncCallback,
	stateObject As Object
) As IAsyncResult

Parameters

array
Type: System.Byte()

The buffer containing data to write to the current stream.

offset
Type: System.Int32

The zero-based byte offset in array at which to begin copying bytes to the current stream.

numBytes
Type: System.Int32

The maximum number of bytes to write.

userCallback
Type: System.AsyncCallback

The method to be called when the asynchronous write operation is completed.

stateObject
Type: System.Object

A user-provided object that distinguishes this particular asynchronous write request from other requests.

Return Value

Type: System.IAsyncResult

An object that references the asynchronous write.

Exception Condition
ArgumentException

array length minus offset is less than numBytes.

ArgumentNullException

array is null.

ArgumentOutOfRangeException

offset or numBytes is negative.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

The stream is closed.

IOException

An I/O error occurred.

In the .NET Framework 4 and earlier versions, you have to use methods such as BeginWrite and EndWrite to implement asynchronous file operations. These methods are still available in the .NET Framework 4.5 to support legacy code; however, the new async methods, such as ReadAsync, WriteAsync, CopyToAsync, and FlushAsync, help you implement asynchronous file operations more easily.

EndWrite must be called exactly once on every IAsyncResult from BeginWrite. EndWrite will block until the I/O operation has completed.

This method overrides BeginWrite.

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, FileStream opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.

If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginWrite. 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 EndWrite.

Multiple simultaneous asynchronous requests render the request completion order uncertain.

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

This code example is part of a larger example provided for the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.

Shared Sub Main()

    ' Create a synchronization object that gets 
    ' signaled when verification is complete.
    Dim manualEvent As New ManualResetEvent(False)

    ' Create random data to write to the file.
    Dim writeArray(100000) As Byte
    Dim randomGenerator As New Random()
    randomGenerator.NextBytes(writeArray)

    Dim fStream As New FileStream("Test#@@#.dat", _
        FileMode.Create, FileAccess.ReadWrite, _
        FileShare.None, 4096, True)

    ' Check that the FileStream was opened asynchronously.
    If fStream.IsAsync = True
        Console.WriteLine("fStream was opened asynchronously.")
    Else
        Console.WriteLine("fStream was not opened asynchronously.")
    End If

    ' Asynchronously write to the file.
    Dim asyncResult As IAsyncResult = fStream.BeginWrite( _
        writeArray, 0, writeArray.Length, _
        AddressOf EndWriteCallback , _
        New State(fStream, writeArray, manualEvent))

    ' Concurrently do other work and then wait
    ' for the data to be written and verified.
    manualEvent.WaitOne(5000, False)
End Sub

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: