_EXAMINING DELPHI 2.0_ by Ted Faison Listing One TMyThread = class(TThread) protected procedure Execute; override; public constructor Create(your parameters} ); end; Listing Two type TMyForm = class(TForm) private LengthyOperation: TMyThread; end; Listing Three procedure TMyForm.StartButtonClick(Sender: TObject); begin LengthyOperation:= TMyThread.Create( your parameter list); end; Listing Four procedure TMyForm. AbortButtonClick(Sender: TObject); begin LengthyOperation.Terminate; end; Listing Five procedure TShape.Paint; begin with Canvas do Rectangle(100, 100, 200, 200); end; Listing Six procedure TMyForm.WndProc(var Message: TMessage); begin case Message.Msg of WM_LBUTTONDOWN, WM_CHAR: {do something} Exit; end; inherited; end; Listing Seven procedure TForm1.Button1Click(Sender: TObject); begin end; Listing Eight unit ThreadTest; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TThreadBouncingObject = class(TThread) private ContainmentBox: TPaintBox; BoundsCheckBox: TRect; P: TPoint; XIncrement, YIncrement: Integer; procedure UpdateScreen; protected procedure Execute; override; public constructor Create(Box: TPaintBox); end; const WIDTH = 10; HEIGHT = 10; implementation {$R *.DFM} constructor TThreadBouncingObject.Create(Box: TPaintBox); begin inherited Create(True); ContainmentBox := Box; BoundsCheckBox := Rect(0, 0, Box.Width - WIDTH, Box.Height - HEIGHT); P.X := Random(ContainmentBox.Width - WIDTH); P.Y := Random(ContainmentBox.Height - HEIGHT); XIncrement := 1; YIncrement := 1; end; procedure TThreadBouncingObject.Execute; begin repeat begin {update the X position} P.X := P.X + XIncrement; if (XIncrement > 0) and (P.X >= BoundsCheckBox.Right) or (XIncrement < 0) and (P.X <= BoundsCheckBox.Left) then XIncrement := -XIncrement; {update the Y position} P.Y := P.Y + YIncrement; if (YIncrement > 0) and (P.Y >= BoundsCheckBox.Bottom) or (YIncrement < 0) and (P.Y <= BoundsCheckBox.Top) then YIncrement := -YIncrement; UpdateScreen; Sleep(10); {pause briefly} end until Terminated; end; procedure TThreadBouncingObject.UpdateScreen; begin ContainmentBox.Canvas.Rectangle(P.X, P.Y, P.X + WIDTH, P.Y + HEIGHT); end; end. Listing Nine unit TestForm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, ThreadTest, StdCtrls; type TForm3Threads = class(TForm) Panel1: TPanel; PaintBox1: TPaintBox; PaintBox2: TPaintBox; PaintBox3: TPaintBox; ButtonStart: TButton; ButtonStop: TButton; procedure FormDestroy(Sender: TObject); procedure ButtonStartClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ButtonStopClick(Sender: TObject); private Object1: TThreadBouncingObject; Object2: TThreadBouncingObject; Object3: TThreadBouncingObject; public { Public declarations } end; var Form3Threads: TForm3Threads; implementation {$R *.DFM} procedure TForm3Threads.FormDestroy(Sender: TObject); begin Object1.Terminate; Object1.Free; Object2.Terminate; Object2.Free; Object3.Terminate; Object3.Free; end; procedure TForm3Threads.ButtonStartClick(Sender: TObject); begin Object1.Resume; Object2.Resume; Object3.Resume; end; procedure TForm3Threads.FormCreate(Sender: TObject); begin Randomize; {for the object initial positions} Object1 := TThreadBouncingObject.Create(PaintBox1); Object2 := TThreadBouncingObject.Create(PaintBox2); Object3 := TThreadBouncingObject.Create(PaintBox3); end; procedure TForm3Threads.ButtonStopClick(Sender: TObject); begin Object1.Suspend; Object2.Suspend; Object3.Suspend; end; end.