DELPHI 多线程(TThread类的实现)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 type 10 TMyThread = class(TThread) 11 private 12 { Private declarations } 13 protected 14 procedure Execute; override; 15 procedure Run; 16 end; 17 TForm1 = class(TForm) 18 btn1: TButton; 19 procedure btn1Click(Sender: TObject); 20 private 21 { Private declarations } 22 public 23 { Public declarations } 24 end; 25 26 27 28 var 29 Form1: TForm1; 30 31 32 implementation 33 34 {$R *.dfm} 35 36 var 37 MyThread:TMyThread; 38 39 procedure TMyThread.Execute; 40 begin 41 { Place thread code here } 42 FreeOnTerminate:=True; {加上这句线程用完了会自动注释} 43 Run; 44 end; 45 46 procedure TMyThread.Run; 47 var 48 i:integer; 49 begin 50 for i := 0 to 500000 do 51 begin 52 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 begin 60 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 type 10 TMyThread = class(TThread) 11 private 12 { Private declarations } 13 protected 14 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 private 23 { Private declarations } 24 public 25 { Public declarations } 26 end; 27 28 29 30 var 31 Form1: TForm1; 32 33 34 implementation 35 36 {$R *.dfm} 37 38 uses SyncObjs; 39 40 var 41 MyThread:TMyThread; {声明线程} 42 CS:TCriticalSection; {声明临界体} 43 44 45 procedure TMyThread.Execute; 46 begin 47 { Place thread code here } 48 FreeOnTerminate:=True; {加上这句线程用完了会自动注释} 49 Run; {运行} 50 end; 51 52 procedure TMyThread.Run; 53 var 54 i:integer; 55 begin 56 CS.Enter; {我要用了,其它人等下} 57 for i := 0 to 100 - 1 do 58 begin 59 Form1.lst1.Items.Add(IntToStr(i)); 60 end; 61 CS.Leave; {我用完了,下一个} 62 end; 63 64 procedure TForm1.btn1Click(Sender: TObject); 65 begin 66 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 begin 75 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类的实现)的主要内容,如果未能解决你的问题,请参考以下文章