以下全局Hook线程占用太多CPU,除非我在那里添加Sleep(10), 还有其他解决方案而不是睡眠(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;