Asynchronous I/O

By default, the .NET framework use synchronous I/O, preventing the program to continue it's execution while the I/O is performed. Asynchronous I/O allows you to continue the execution and monitor the I/O execution.

In order to indicate to the framework that you want to use asynchronous I/O, you must create a Stream object using a constructor that allow you to use asynchronous I/O. For the FileStream object, this constructor is:

Failed to execute the [code] macro

To use asynchronous I/O, before calling Read or Write, you must call BeginRead or BeginWrite.

The prototype of those methods are:

Failed to execute the [code] macro

There are two ways to be informed when the operation is finished, you can call EndRead or EndWrite passing it the IAsyncResult corresponding to the I/O request you made, or you can pass BeginRead and BeginWrite a callback method to be called when the I/O completes.
If you use the callback approach, the callback method should call EndRead or EndWrite to figure out how many bytes were read or written.

The following code sample demonstrate how to perform an asynchronous I/O on a file stream using the callback method:

Failed to execute the [code] macro

Continue to: