给你一个完整的例子,我调试通过:unit Unit2;interfaceuses Classes,Dialogs;type TRegThread = class(TThread) private { Private declarations } FTempS: String; procedure doR; procedure R(temps: string); protected procedure Execute; override; end;implementation{ 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 TRegThread.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ TRegThread } procedure TRegThread.R(temps: string); begin Ftemps := temps; showmessage('进入R'); Synchronize(doR);end; procedure TRegThread.doR; begin showmessage('进入doR境界'); end;procedure TRegThread.Execute; begin { Place thread code here } R('here!'); end;end. unit Unit1;interfaceuses 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;var Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin TRegThread.Create(FALSE); // OnTerminate := ThreadDone; end;end.
begin
Ftemps := temps;
showmessage('进入R');
Synchronize(TRThread.doR);
end;procedure TRThread.doR;
begin
showmessage('进入doR境界');
end;
begin
Ftemps := temps;
showmessage('进入R');
//应该是这样,创建trthread实例t,然后调用t.dor
Synchronize(t.doR);
end;procedure TRThread.doR;
begin
showmessage('进入doR境界');
end;
我已经在主线程中
with TRThread.Create() do
OnTerminate := ThreadDone;
了。那么这个时候还需要怎么创建实例呢?我看demo中的例子是不需要额外创建的啊。
with t do
OnTerminate := ThreadDone;......
procedure TRegThread.R(temps: string);
begin
Ftemps := temps;
showmessage('进入R');
//应该是这样,创建trthread实例t,然后调用t.dor
Synchronize(t.doR);
end;procedure TRThread.doR;
begin
showmessage('进入doR境界');
end;
procedure TRThread.doR;
begin
showmessage('进入doR境界');
end;改为:
procedure TRegThread.doR;
begin
showmessage('进入doR境界');
end;即OK!
Classes,Dialogs;type
TRegThread = class(TThread)
private
{ Private declarations }
FTempS: String;
procedure doR;
procedure R(temps: string);
protected
procedure Execute; override;
end;implementation{ 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 TRegThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TRegThread }
procedure TRegThread.R(temps: string);
begin
Ftemps := temps;
showmessage('进入R'); Synchronize(doR);end;
procedure TRegThread.doR;
begin
showmessage('进入doR境界');
end;procedure TRegThread.Execute;
begin
{ Place thread code here }
R('here!');
end;end.
unit Unit1;interfaceuses
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;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin
TRegThread.Create(FALSE);
// OnTerminate := ThreadDone;
end;end.