unit Unit2;interfaceuses Classes,SysUtils;type Tmythread = class(TThread) private { Private declarations } protected function sum(x,y,z:integer):integer; procedure Execute; override; end;implementationuses Unit1;{ Tmythread }procedure Tmythread.Execute; begin { Place thread code here } while not Terminated do unit1.Form1.Edit1.Text:=inttostr(sum(1,2,3)); end;function Tmythread.sum(x, y, z: integer): integer; begin result:=x+y+z; end;end.unit1 .... procedure TForm1.Button1Click(Sender: TObject); begin with Tmythread.Create(true) do begin FreeOnTerminate:=true; Resume; end; end;
//线程类 unit Unit2;interfaceuses Classes,SysUtils;type TSumThread = class(TThread) private { Private declarations } FSumResult:Integer; procedure SumProc(I,J,K:Integer); procedure UpdateCaption; protected procedure Execute; override; end;implementationuses Unit1;procedure TSumThread.Execute; begin { Place thread code here } FreeOnTerminate:=True; SumProc(1,2,3); Synchronize(UpdateCaption); end;procedure TSumThread.UpdateCaption; begin Form1.Edit1.Text:=IntToStr(FSumResult) end;procedure TSumThread.SumProc(I,J,K:Integer); begin FSumResult:=I+J+K; end;end. //主程序 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; 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); var TestThread:TSumThread; begin TestThread:=TSumThread.Create(True); TestThread.Resume; end;end.
感谢啊,我正好也打算学delphi的多线程
Classes,SysUtils;type
Tmythread = class(TThread)
private
{ Private declarations }
protected
function sum(x,y,z:integer):integer;
procedure Execute; override;
end;implementationuses Unit1;{ Tmythread }procedure Tmythread.Execute;
begin
{ Place thread code here }
while not Terminated do
unit1.Form1.Edit1.Text:=inttostr(sum(1,2,3));
end;function Tmythread.sum(x, y, z: integer): integer;
begin
result:=x+y+z;
end;end.unit1
....
procedure TForm1.Button1Click(Sender: TObject);
begin
with Tmythread.Create(true) do
begin
FreeOnTerminate:=true;
Resume;
end;
end;
//线程类
unit Unit2;interfaceuses
Classes,SysUtils;type
TSumThread = class(TThread)
private
{ Private declarations }
FSumResult:Integer;
procedure SumProc(I,J,K:Integer);
procedure UpdateCaption;
protected
procedure Execute; override;
end;implementationuses Unit1;procedure TSumThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate:=True;
SumProc(1,2,3);
Synchronize(UpdateCaption);
end;procedure TSumThread.UpdateCaption;
begin
Form1.Edit1.Text:=IntToStr(FSumResult)
end;procedure TSumThread.SumProc(I,J,K:Integer);
begin
FSumResult:=I+J+K;
end;end.
//主程序
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
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);
var
TestThread:TSumThread;
begin
TestThread:=TSumThread.Create(True);
TestThread.Resume;
end;end.
VCL要求所有的用户界面控制要发生在一个应用程序的主线程的环境中 (线程安全的TCanvas类除外),故需要用Synchronize,通过它可以让线程的一些方法在主线程中执行