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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.