首页 > 代码库 > DELPHI 多线程(TThread类的实现)
DELPHI 多线程(TThread类的实现)
DELPHI 多线程(TThread类的实现)
先新建一个普通的工程,再新建一个线程类File>>New>>Othre>>Delphi File>Thread Object,取个名字,DELPHI会自动生成一个单元,我们只需往里简单添加功能代码,和在要使用的单元里实例引用即可。
为了节省篇幅,现把TMyThread类集成主窗体单元里,在主窗体里声明类也是可以的。
1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type10 TMyThread = class(TThread)11 private12 { Private declarations }13 protected14 procedure Execute; override;15 procedure Run;16 end;17 TForm1 = class(TForm)18 btn1: TButton;19 procedure btn1Click(Sender: TObject);20 private21 { Private declarations }22 public23 { Public declarations }24 end;25 26 27 28 var29 Form1: TForm1;30 31 32 implementation33 34 {$R *.dfm}35 36 var37 MyThread:TMyThread;38 39 procedure TMyThread.Execute;40 begin41 { Place thread code here }42 FreeOnTerminate:=True; {加上这句线程用完了会自动注释}43 Run;44 end;45 46 procedure TMyThread.Run;47 var48 i:integer;49 begin50 for i := 0 to 500000 do51 begin52 Form1.Canvas.Lock;53 Form1.Canvas.TextOut(10,10,IntToStr(i));54 Form1.Canvas.Unlock;55 end;56 end;57 58 procedure TForm1.btn1Click(Sender: TObject);59 begin60 MyThread:=TMyThread.Create(False); {实例化这个类,为False时立即运行,为True时可加MyThread.Resume用来启动}61 end;
CriticalSection(临界区)
uses SyncObjs;用TCriticalSection类的方法处理。
1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type10 TMyThread = class(TThread)11 private12 { Private declarations }13 protected14 procedure Execute; override; {执行}15 procedure Run; {运行}16 end;17 TForm1 = class(TForm)18 btn1: TButton;19 lst1: TListBox;20 procedure btn1Click(Sender: TObject);21 procedure FormDestroy(Sender: TObject);22 private23 { Private declarations }24 public25 { Public declarations }26 end;27 28 29 30 var31 Form1: TForm1;32 33 34 implementation35 36 {$R *.dfm}37 38 uses SyncObjs;39 40 var41 MyThread:TMyThread; {声明线程}42 CS:TCriticalSection; {声明临界体}43 44 45 procedure TMyThread.Execute;46 begin47 { Place thread code here }48 FreeOnTerminate:=True; {加上这句线程用完了会自动注释}49 Run; {运行}50 end;51 52 procedure TMyThread.Run;53 var54 i:integer;55 begin56 CS.Enter; {我要用了,其它人等下}57 for i := 0 to 100 - 1 do58 begin59 Form1.lst1.Items.Add(IntToStr(i));60 end;61 CS.Leave; {我用完了,下一个}62 end;63 64 procedure TForm1.btn1Click(Sender: TObject);65 begin66 CS:=TCriticalSection.Create; {实例化临界体}67 MyThread:=TMyThread.Create(False); {实例化这个类,为False时立即运行,为True时可加MyThread.Resume用来启动}68 MyThread:=TMyThread.Create(False);69 MyThread:=TMyThread.Create(False);70 end;71 72 73 procedure TForm1.FormDestroy(Sender: TObject);74 begin75 CS.Free;{释放临界体}76 end;77 78 end.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
等待函数 PS:
function WaitForSingleObject(
hHandle: THandle; {要等待的对象句柄}
dwMilliseconds: DWORD {等待的时间, 单位是毫秒}
): DWORD; stdcall; {返回值如下:}
WAIT_OBJECT_0 {等着了, 本例中是: 等的那个进程终于结束了}
WAIT_TIMEOUT {等过了点(你指定的时间), 也没等着}
WAIT_ABANDONED {好不容易等着了, 但人家还是不让咱执行; 这一般是互斥对象}
//WaitForSingleObject 的第二个参数一般给常数值 INFINITE, 表示一直等下去, 死等.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DELPHI 多线程(TThread类的实现)