在一个Application中, 执行file\new\other...从对话框中选择Thread object 就创建了一个线程,输入线程的名字就行了,在过程:procedure 你输入的线程名.Execute;中写入线程要执行的代码就行了,然后在主窗体中引用线程所在的单元并加入一个button1: procedure TForm1.Button1Click(Sender: TObject); var sss:ss; begin sss:=ss.Create(false); end; 就可以了;
procedure ss.Execute; begin freeontimernate:=true;//最好加入这行代码,线程执行完后自动释放其所占的资源 { Place thread code here } end;
----------------------------file-new-other --threadobject unit Unit3;interfaceuses Classes;type aa = class(TThread) private { Private declarations } protected procedure Execute; override; end;implementationuses Unit1;{ Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure aa.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ aa }procedure aa.Execute; begin { Place thread code here } synchronize(form1.Button1.Click); //------------------------加入一行代码 end;end.
不要试图在线程中做改变界面的操作,如:设置edit的值;等以后对线程熟悉了再深入吧
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB, Mask;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses Unit3;{$R *.dfm} var a:aa; procedure TForm1.Button1Click(Sender: TObject); begin while true do ; //----------------------------------死掉 end;procedure TForm1.Button2Click(Sender: TObject); begin a:=aa.Create(false);//--------------------------------执行后程序主体还活着 end;end.
mythread=class(TThread) procedure mythread.excute; begin ... end;要用的地方,CREATE一下线程就自己运行了
执行file\new\other...从对话框中选择Thread object 就创建了一个线程,输入线程的名字就行了,在过程:procedure 你输入的线程名.Execute;中写入线程要执行的代码就行了,然后在主窗体中引用线程所在的单元并加入一个button1:
procedure TForm1.Button1Click(Sender: TObject);
var
sss:ss;
begin
sss:=ss.Create(false);
end;
就可以了;
begin
freeontimernate:=true;//最好加入这行代码,线程执行完后自动释放其所占的资源
{ Place thread code here }
end;
unit Unit3;interfaceuses
Classes;type
aa = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;implementationuses Unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure aa.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ aa }procedure aa.Execute;
begin
{ Place thread code here }
synchronize(form1.Button1.Click); //------------------------加入一行代码
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Mask;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit3;{$R *.dfm}
var a:aa;
procedure TForm1.Button1Click(Sender: TObject);
begin
while true do ; //----------------------------------死掉
end;procedure TForm1.Button2Click(Sender: TObject);
begin
a:=aa.Create(false);//--------------------------------执行后程序主体还活着
end;end.
procedure mythread.excute;
begin
...
end;要用的地方,CREATE一下线程就自己运行了