Asynchronous operations take place independent of the current process. After BeginRead is called, control immediately returns to the current process thread of execution.
You specify the Count of bytes that will be read into the Bytes array from StartIndex.
You specify the procedure that is called when the read is completed. It must have the following form :
Procedure Name (AsyncResult : IAsyncResult);
You pass one of your objects ReadControl to the read to allow control of multiple threads. This gets returned in the IAsyncResult callback method parameter.
You need not rely on the return method - you can instead wait for the read to finish by calling EndRead. But this tends to negate the meaning of the asynchronous process in the first place.
|Microsoft MSDN Links|
|A simple example|
Stream : System.IO.FileStream;
ByteArray : Array of Byte;
procedure ReadDone(AsyncResult : IAsyncResult);
Index : Integer;
Console.WriteLine('Read is finished :');
// Display our array contents
for Index := 0 to Length(ByteArray)-1 do
// Create our File Stream
Stream := System.IO.FileStream.Create('C:\DelphiBasics.txt', FileMode.Create);
// Write to the stream
// Move to the start of the stream
// Declare our array to hold the returned data
// Read from the stream asynchronously
Stream.BeginRead(ByteArray, 0, 10, ReadDone, TObject.Create);
// Close the file stream
Read is finished :|