How to do it...

Follow these step-by-step instructions to synchronize shared resources with TMonitor:

  1. Create a new VCL Forms Application (navigate to File | New | VCL Forms Application).
  1. Drop a TButton, TListBox, and TTimer component on the form.
  2. Name the TButton component as btnStart, and change the value of Caption to Multiple writes on a shared file.
  3. Add a new unit to the project, call it FileWriterThreadU.pas, and add the following code to it:
unit FileWriterThreadU; 
 
interface 
 
uses 
  System.Classes, System.SyncObjs, System.SysUtils, System.IOUtils; 
 
type 
  TThreadHelper = class helper for TThread 
  public 
    function WaitFor(ATimeout: Cardinal): LongWord; platform; 
  end; 
 
  TFileWriterThread = class(TThread) 
  private FStreamWriter: TStreamWriter; ...

Get Delphi Cookbook - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.