好象没有反应啊???unit frameUnit;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons;type framecontrol = class(TThread) private { Private declarations } protected procedure Execute; override; procedure OnTerminate; public constructor create(suspended:boolean); end;implementationuses commainform;{ Important: Methods and properties of objects in VCL can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure framecontrol.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ framecontrol }constructor framecontrol.create(suspended:boolean); begin inherited create(suspended); freeonterminate:=true; end;procedure framecontrol.Execute; begin { Place thread code here } while form1.sysflag do begin application.ProcessMessages; end; showmessage('Hi!'); end;procedure framecontrol.OnTerminate; begin form1.threadflag:=false; end;end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Buttons;type
framecontrol = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure OnTerminate;
public
constructor create(suspended:boolean);
end;implementationuses commainform;{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure framecontrol.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ framecontrol }constructor framecontrol.create(suspended:boolean);
begin
inherited create(suspended);
freeonterminate:=true;
end;procedure framecontrol.Execute;
begin
{ Place thread code here }
while form1.sysflag do begin
application.ProcessMessages;
end;
showmessage('Hi!');
end;procedure framecontrol.OnTerminate;
begin
form1.threadflag:=false;
end;end.
如果可以,你可以通过消息的方式main---->(PostThreadMessage)-->Thread; Thread->(Postmessage)->Main
const
WM_ThreadState = WM_User + 1000;
WM_ThreadClose = WM_User + 1001;
WM_ThreadMsg = WM_User + 1002; WM_ParentState = WM_User + 2000;
WM_ParentMsg = WM_User + 2001;type
TMyThread = class(TThread)
private
FParentHandle: THandle;
protected
procedure Execute; override;
public
constructor Create(AParentHandle: THandle);
end; TForm1 = class(TForm)
procedure Button1OnClick(Sender: TObject);
procedure Button2OnClick(Sender: TObject);
procedure Button3OnClick(Sender: TObject);
private
FThread: TMyThread;
procedure OnTerminate(Sender: TObject);
procedure MsgState(var Msg: TMessage);message WM_ParentState;
procedure MsgRecv(var Msg: TMessage); message WM_ParentMsg;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}{ TMyThread }constructor TMyThread.Create(AParentHandle: THandle);
begin
FreeOnTerminate := True;
inherited Create(False); //马上Execute
end;procedure TMyThread.Execute;
var
Msg: TMsg;
begin
PeekMessage(Msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
while not Terminated do
begin
if PeekMessage(Msg, 0, WM_USER, WM_USER, PM_REMOVE) then
case Msg.message of
WM_ThreadState: PostMessage(FParentHandle, WM_ParentState, 0, 0);
WM_ThreadClose: Terminate;
WM_ThreadMsg: PostMessage(FParentHandle, WM_ParentMsg, 0, Integer(Pointer(PChar('Hello'))));
end;
end;
end;{ TForm1 }procedure TForm1.Button1OnClick(Sender: TObject);
begin
FThread := TMyThread.Create(Handle);
FThread.OnTerminate := OnTerminate;
end;procedure TForm1.Button2OnClick(Sender: TObject);
begin
if Assigned(FThread) then
PostThreadMessage(FThread.ThreadID, WM_ThreadState, 0, 0);
end;procedure TForm1.Button3OnClick(Sender: TObject);
begin
if Assigned(FThread) then
PostThreadMessage(FThread.ThreadID, WM_ThreadMsg, 0, 0);
end;procedure TForm1.MsgRecv(var Msg: TMessage);
var
S: string;
begin
S := PChar(Pointer(Msg.LParam));
ShowMessage(S);
end;procedure TForm1.MsgState(var Msg: TMessage);
begin
ShowMessage('Thread is Running...');
end;procedure TForm1.OnTerminate(Sender: TObject);
begin
ShowMessage('Thread is Termitnate');
end;