将 OnTerminate 指向一个类过程
unit Unit1;
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 }
procedure a(Sender : TObject) ;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Unit2 ;
procedure TForm1.a(Sender : TObject) ;
begin
ShowMessage('1234') ;
end ;
procedure TForm1.Button1Click(Sender: TObject);
var
t : TThTest ;
begin
t := TThTest.Create(False) ;
with t do
if Suspended then Resume;
t.OnTerminate := a ;
end;
end.
--------------------------------------------
TheThread :
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TThTest = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure Show ;
public
Constructor Create(B : Boolean) ;
end;
implementation
uses unit1 ;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TThTest.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TThTest }
Constructor TThTest.create(B : Boolean) ;
begin
Inherited Create(False) ;
end ;
procedure TThTest.Execute;
begin
{ Place thread code here }
Synchronize(Show)
end;
procedure TThTest.Show ;
var
i : Integer ;
DC : HDC ;
s : String ;
begin
DC := GetDC(Form1.Handle) ;
for i := 0 to 100000 do
begin
S:=Inttostr(i);
Textout(DC,10,10,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
end ;
end.