初级问题: 怎么创建一个线程呀?
对线程我是第一次用, 对线程的概念还算了解,但是怎么创建一个线程呀?有例子最好了.谢谢朋友!!
对线程我是第一次用, 对线程的概念还算了解,但是怎么创建一个线程呀?有例子最好了.谢谢朋友!!
解决方案 »
- Statusbar的AutoHint问题
- 用OpenDataSource怎么在客户端导入excel数据到SQLServer呢?
- 简单的SQL语句,大家来帮帮忙
- 小弟初次提问,望高手多多指点?
- 判断是否是文件夹(directory),就是给一个字符串,判断是否是directory(帖子内容空)
- NMSMTP1控件的问题!
- free和destroy谁包含谁
- 在线程中动态创建SOCKET和使用SOCKET过程中遇到了一些问题,希望有相关处理经验朋友给点帮助!
- 100大分寻求扬声器的“嘀嘀”声音的制作
- 怎样用最简单的方法制作帮助!windows风格!
- 哪位大哥知道这是什么问题!?
- 菜鸟终于体会到了软件开发第二和第三重要要的技术是建模技术和测试技术.有很强的欲望想学习他们。 :)
执行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一下线程就自己运行了