Home  |  Delphi .net Home  |  System.IO.FileStream  |  Lock Method
Lock  
Method  
Lock a section of the current stream to prevent others writing to it
FileStream Class
System.IO NameSpace
NotCF1.  Procedure Lock ( StartIndex:Int64StartIndex : Int64; Count : Int64; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
Count bytes from StartIndex are locked. This prevents other, concurrent processes writing to them.
Notes
This is somewhat beyond Delphi Basics, so the example merely illustrates the syntax.
Microsoft MSDN Links
System.IO
System.IO.FileStream
 
 
Illustrating the syntax
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Stream : System.IO.FileStream;
  i      : Integer;

begin
  // Open the file for writing
  Stream := System.IO.FileStream.Create('C:\DelphiBasics.txt',
                                        FileMode.Create);

  // Write to the file
  Stream.WriteByte(65);  // Chr(65) = 'A'
  Stream.WriteByte(66);  // Chr(66) = 'B'
  Stream.WriteByte(67);  // Chr(67) = 'C'
  Stream.WriteByte(68);  // Chr(68) = 'D'
  Stream.WriteByte(69);  // Chr(69) = 'E'

  // Lock the last 3 bytes for update
  Stream.Lock(2, 3);

  // Now any concurrent process will be unable to write to
  // these last 3 bytes of the file.

  // ... Run a concurrent process at this point

  // Unlock the file
  Stream.Unlock(2, 3);

  // Close the stream
  Stream.Close;

  Console.Readline;
end.
Show full unit code
  
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author