unit threadUnit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; TSendThread = class(TThread) private Msg: string; FException: Exception; procedure OnFinish(Sender: TOBject); protected procedure Execute; override; procedure DoHandleException; public isclosing: Boolean; Progress: Integer; procedure HandleException; end; var Form1: TForm1; emailthread : TSendThread; implementation {$R *.dfm} procedure TSendThread.Execute; var msg: string; TS : TStringList; i : integer; begin FException := nil; try TS := TStringList.create; for i := 0 to 10000 do TS.Add('asdfsdafsdafsdafsdafsdafsdafsdafsda'); TS.Free; TS := nil; beep; Terminate; except HandleException; Terminate; end; beep; // emailthread.free; //< 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0); // Now show the exception if FException is Exception then Application.ShowException(FException) else SysUtils.ShowException(FException, nil); end; procedure TSendThread.HandleException; begin // This function is virtual so you can override it and add your own functionality. FException := Exception(ExceptObject); try if not (FException is EAbort) then // Don't show EAbort messages Synchronize(DoHandleException); finally FException := nil; terminate; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if EmailThread <> nil then if not EmailThread.terminated then begin showmessage('Emails are already in process of being sent - please wait.'); exit; end; EmailThread := TSendThread.Create(True); EmailThread.FreeOnTerminate := false; EmailThread.OnTerminate := EmailThread.onfinish; Emailthread.Resume; end; end.