线程单元:
unit Unit2;
interface
uses
Windows, Messages, Classes;
const
//自军定义消息;
MSG_MYMSG = WM_USER + 100;
type
TMyThread = class(TThread)
private
FMainHandle: THandle;
//调用本线程的主线程句柄;
protected
procedure Execute;
override;
public
constructor Create(AHandle: THandle);
end;
implementation
{ TMyThread }
constructor TMyThread.Create(AHandle: THandle);
begin
FMainHandle := AHandle;
inherited Create(True);
end;
procedure TMyThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate := True;
while not Terminateddo
begin
//这里放你的处理;
if FMainHandle <> 0 then
//反馈进度;
PostMessage(FMainHandle, MSG_MYMSG, 0, 0);
end;
end;
end.
窗体单元:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit2;
type
TForm1 = class(TForm)
private
protected
procedure WndProc(var Message: TMessage);
override;
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
{ TForm1 }
procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg = MSG_MYMSG then
begin
//在这里处理消息;
end
else
inherited;
end;
end.