Home  |  Delphi .net Home  |  System.IO.FileStream  |  BeginWrite Method
  
BeginWrite  
Method  
Starts an asynchronous stream write operation
FileStream Class
System.IO NameSpace
CF1.  Function BeginWrite ( Bytes : Array of Bytes; StartIndex : Integer; Count : Integer; CompletionMethod : AsyncCallBack; WriteControl : Object ) : AsyncResult:AsyncResult;
CF : Methods with this mark are Compact Framework Compatible
Description
Asynchronous operations take place independent of the current process. After BeginWrite is called, control immediately returns to the current process thread of execution.
 
You specify the Count of bytes that will be written to the current stream from the Bytes array from StartIndex.
 
You specify the procedure that is called when the write is completed. It must have the following form :
 
Procedure Name (AsyncResult : IAsyncResult);
 
You pass one of your objects WriteControl to the write 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 write to finish by calling EndWrite. But this tends to negate the meaning of the asynchronous process in the first place.
Notes
The example program seems to be suffering from a timing problem - the expected stream contents are not displayed. The author does not understand why this is the case.
Microsoft MSDN Links
System.IO
System.IO.FileStream
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO,
  System.Runtime.Remoting.Messaging;

var
  Stream    : System.IO.FileStream;
  ByteArray : Array of Byte;
  ByteAsInt : Integer;
  AsyncRes  : IAsyncResult;
  Index     : Integer;

procedure WriteDone(AsyncResult : IAsyncResult);
var
  Index : Integer;
begin
  Console.WriteLine('Write is finished :');
  Console.WriteLine;

  // Display the stream contents
  Stream.Seek(0, SeekOrigin.Begin);
  ByteAsInt := Stream.ReadByte;

  while ByteAsInt > 0 do
  begin
    Console.WriteLine(Char(ByteAsInt));
    ByteAsInt := Stream.ReadByte;
  end;
end;

begin
  // Create our File Stream
  Stream := System.IO.FileStream.Create('C:DelphiBasics.txt', FileMode.Create);

  // Create our byte array
  SetLength(ByteArray, 10);

  // And fill it
  ByteArray[0] := 65; // Ord(65) = 'A'
  ByteArray[1] := 66;
  ByteArray[2] := 67;
  ByteArray[3] := 68;
  ByteArray[4] := 69;
  ByteArray[5] := 70;
  ByteArray[6] := 71;
  ByteArray[7] := 72;
  ByteArray[8] := 73;
  ByteArray[9] := 74;

  // Write to the stream from the array asynchronously
  AsyncRes := Stream.BeginWrite(ByteArray, 0, 10, WriteDone, TObject.Create);

  // Close the file stream
  Stream.Close;

  Console.Readline;
end.
   The output of this program was expected to be the following, but only the first line was returned.
  
   Write is finished :
  
   A
   B
   C
   D
   E
   F
   G
   H
   I
   J
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author