All the programs you’ve looked at so far perform synchronous I/O , meaning that while your program is reading or writing, all other activity is stopped. It can take a long time (relatively speaking) to read data to or from the backing store, especially if the backing store is a slow disk or (horrors!) a slow network.
With large files, or when reading or writing across the network,
which allows you to begin a read and then turn your attention to
other matters while the Common Language Runtime (CLR) fulfills
your request. The .NET Framework provides asynchronous I/O through
BeginRead( ) and
The sequence is to call
BeginRead( ) on your file
and then to go on to other, unrelated work while the read progresses
in another thread. When the read completes, you are notified via a
callback method. You can then process the data that was read, kick
off another read, and then go back to your other work.
In addition to the three parameters you’ve used in
the binary read (the buffer, the offset, and how many bytes to read),
BeginRead( ) asks for a
The delegate is an optional callback method, which, if provided, is
called when the data is read. The state object is also optional. In
this example, pass in
null for the state object.
The state of the object is kept in the member variables of the test
You are free to put any object you like in the state parameter, and you can retrieve ...