如题,在子线程中可以设热键吗?或者是我哪里作的不对?这套东西写在主线程里就没有问题啊。
另外我在Execute中用while占用系统资源十分的大,请问我应该用什么办法来替代解决呢?谢谢
程序如下子线程UNIT中是这样写的:
type
THotkeyThread = class(TThread)
private
atomF4: atom;
procedure hotkey(var msg:tmessage); message wm_hotkey;
protected
procedure Execute; override;
public
constructor Create; overload;
destructor Destroy; override;
procedure WriteisSpare;
end;
implementation
{ THotkeyThread }constructor THotkeyThread.Create;
begin
inherited Create(True);
atomF4:=globaladdatom('hot key F4');
RegisterHotKey(handle,atomF4,0,vk_F4);
Resume;
end;destructor THotkeyThread.Destroy;
begin
inherited;
globalDeleteatom(atomF4);
end;procedure THotkeyThread.Execute;
begin
while not FreeOnTerminate do ;
end;procedure THotkeyThread.hotkey(var msg: tmessage);
var
i: Integer;
s: string;
URL: PChar;
begin
if (msg.LParamHi=VK_F4) then
begin
Synchronize(WriteisSpare);
end;
end;procedure THotkeyThread.WriteisSpare;
begin
FrmMain.isSpare:= true; //设置主线程的标志位
end;
end.主线程UNIT中这样写的:
procedure TFrmMain.BtnStartClick(Sender: TObject);
var
AHotkeyThread: THotkeyThread;
begin
try
AHotkeyThread:= THotkeyThread.Create;
....... //这里需要检测isSpare来判断
.......
.......
.......
finally
AHotkeyThread.FreeOnTerminate:= True;
end;
end;
另外我在Execute中用while占用系统资源十分的大,请问我应该用什么办法来替代解决呢?谢谢
程序如下子线程UNIT中是这样写的:
type
THotkeyThread = class(TThread)
private
atomF4: atom;
procedure hotkey(var msg:tmessage); message wm_hotkey;
protected
procedure Execute; override;
public
constructor Create; overload;
destructor Destroy; override;
procedure WriteisSpare;
end;
implementation
{ THotkeyThread }constructor THotkeyThread.Create;
begin
inherited Create(True);
atomF4:=globaladdatom('hot key F4');
RegisterHotKey(handle,atomF4,0,vk_F4);
Resume;
end;destructor THotkeyThread.Destroy;
begin
inherited;
globalDeleteatom(atomF4);
end;procedure THotkeyThread.Execute;
begin
while not FreeOnTerminate do ;
end;procedure THotkeyThread.hotkey(var msg: tmessage);
var
i: Integer;
s: string;
URL: PChar;
begin
if (msg.LParamHi=VK_F4) then
begin
Synchronize(WriteisSpare);
end;
end;procedure THotkeyThread.WriteisSpare;
begin
FrmMain.isSpare:= true; //设置主线程的标志位
end;
end.主线程UNIT中这样写的:
procedure TFrmMain.BtnStartClick(Sender: TObject);
var
AHotkeyThread: THotkeyThread;
begin
try
AHotkeyThread:= THotkeyThread.Create;
....... //这里需要检测isSpare来判断
.......
.......
.......
finally
AHotkeyThread.FreeOnTerminate:= True;
end;
end;
在主线程里响应热键,挂起某线程工作,处理
然后恢复线程工作。
myhandle:=createthread(nil,0,@yourthread,nil,0,threadid);
if key=#8 then
begin
suspendthread(myhandl);
//do you want
resumthread(myhandle);
end;