使用Delphi里的线程类创建一个线程
本文主要讲解是如何使用Delphi里的线程类创建一个线程,以Delphi 2010环境为参考,创建方法为:
File---new---other---delphi projects---delphi files----Thead object,如图:
会弹出如下对话框,输入类名就行,这里输入TTestThread:

写相关代码如下:
unit uTestThread;
interface
uses
Classes, Windows, SysUtils;
type
TTestThread = class(TThread)
private
FHandle: THandle;
FCaption: string;
FCount: Integer;
{ Private declarations }
procedure DoWork;
protected
procedure Execute; override;
public
constructor Create(AHandle: THandle; ACaption: string; ACount: Integer);
destructor Destroy; override;
end;
implementation
{ TestThread }
constructor TTestThread.Create(AHandle: THandle; ACaption: string;
ACount: Integer);
begin
FHandle := AHandle;
FCaption := ACaption;
FCount := ACount;
inherited Create(False); // 参数为False指线程创建后自动运行,为True则不自动运行
FreeOnTerminate := True; // 执行完毕后自动释放
end;
destructor TTestThread.Destroy;
begin
inherited;
end;
procedure TTestThread.DoWork;
var
I: Integer;
DC: HDC;
TestStr: string;
begin
DC := GetDC(FHandle);
SetBkColor(DC, GetSysColor(COLOR_BTNHIGHLIGHT));
for I := 1 to FCount do
begin
TestStr := FCaption + IntToStr(I);
TextOut(DC, 10, 10, PChar(TestStr), Length(TestStr));
end;
ReleaseDC(FHandle, DC);
end;
procedure TTestThread.Execute;
begin
DoWork;
end;
end.
浙公网安备 33010602011771号