加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 全局钩子线程占用太多CPU,如何修复?

发布时间:2020-12-15 09:07:46 所属栏目:大数据 来源:网络整理
导读:以下全局Hook线程占用太多CPU,除非我在那里添加Sleep(10), 还有其他解决方案而不是睡眠(10毫秒) – 睡眠看起来不像是我的应用程序性能的最佳解决方案.如果我增加太多睡眠,它也不会减慢鼠标的速度. procedure THookThread.Execute; begin hookhandle := SetWi
以下全局Hook线程占用太多CPU,除非我在那里添加Sleep(10),
还有其他解决方案而不是睡眠(10毫秒) – 睡眠看起来不像是我的应用程序性能的最佳解决方案.如果我增加太多睡眠,它也不会减慢鼠标的速度.

procedure THookThread.Execute;
    begin
      hookhandle := SetWindowsHookEx(WH_MOUSE_LL,@LowLevelMouseHook,Hinstance,0);

     while not Terminated do
      begin    
        MessageLoop;
      //  Sleep(10);
      end;

      UnHookWindowsHookEx(hookhandle);
      hookhandle := 0;

    end;

procedure THookThread.MessageLoop;
var
  msg: TMsg;
begin
  while PeekMessage(msg,PM_NOREMOVE) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
end;

解决方法

尝试更像这样的东西:

procedure THookThread.Execute;
var
  msg: TMsg;
  ret: LongInt;
begin
  //create the message queue...
  PeekMessage(msg,WM_USER,PM_NOREMOVE);

  hookhandle := SetWindowsHookEx(WH_MOUSE_LL,0);
  if hookhandle = 0 then RaiseLastOSError; 

  try
    while GetMessage(msg,0) and (not Terminated) do
    begin
      TranslateMessage(msg);
      DispatchMessage(msg);
    end;
  finally
    UnHookWindowsHookEx(hookhandle);
    hookhandle := 0;
  end;
end;

procedure THookThread.Stop;
begin
  Terminate;
  PostThreadMessage(ThreadID,WM_QUIT,0);
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读