Delphi全局钩子线程占用过多CPU,如何有效降低其CPU占用率?
- 内容介绍
- 文章标签
- 相关推荐
本文共计290个文字,预计阅读时间需要2分钟。
以下是对原文的简化
全局Hook线程占用CPU过高,除了我在那里添加了Sleep(10)外,其他解决方案都不是睡眠(10毫秒)——睡眠看起来不是提升我应用程序性能的最佳方法。如果增加睡眠时间,它也不会减慢速度。
以下全局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, 0, 0, 0, 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, 0, WM_USER, WM_USER, PM_NOREMOVE); hookhandle := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseHook, Hinstance, 0); if hookhandle = 0 then RaiseLastOSError; try while GetMessage(msg, 0, 0, 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, 0); end;
本文共计290个文字,预计阅读时间需要2分钟。
以下是对原文的简化
全局Hook线程占用CPU过高,除了我在那里添加了Sleep(10)外,其他解决方案都不是睡眠(10毫秒)——睡眠看起来不是提升我应用程序性能的最佳方法。如果增加睡眠时间,它也不会减慢速度。
以下全局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, 0, 0, 0, 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, 0, WM_USER, WM_USER, PM_NOREMOVE); hookhandle := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseHook, Hinstance, 0); if hookhandle = 0 then RaiseLastOSError; try while GetMessage(msg, 0, 0, 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, 0); end;

