有什么可以导致Windows解开一个较低水平(全球)键盘钩子? [英] What can cause Windows to unhook a low level (global) keyboard hook?

查看:580
本文介绍了有什么可以导致Windows解开一个较低水平(全球)键盘钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须通过和SetWindowsHookEx 安装了 WH_KEYBOARD_LL 一些全局键盘钩子看似随机得到由Windows脱钩。

We have some global keyboard hooks installed via SetWindowsHookEx with WH_KEYBOARD_LL that appear to randomly get unhooked by Windows.

我们证实,他们钩不再附着因为调用 UnhookWindowsHookEx 手柄上的回报。 (也验证了它返回真正当它正常工作)

We verified that they hook was no longer attached because calling UnhookWindowsHookEx on the handle returns false. (Also verified that it returns true when it was working properly)

有似乎没有一以贯之摄制,我听说他们可以得到脱钩由于超时或异常抛出得到,但我都试过只是让它坐在一个断点处理方法一分多钟,以及刚刚抛出一个随​​机异常( C#),它仍然似乎工作。

There doesn't seem to be a consistent repro, I've heard that they can get unhooked due to timeouts or exceptions getting thrown, but I've tried both just letting it sit on a breakpoint in the handling method for over a minute, as well as just throwing a random exception (C#) and it still appears to work.

在我们的回调,我们迅速发布到另一个线程,所以这可能不是问题。我读过有关Windows 7解决方案,因为Windows 7更加积极地显然超时(我们所有正在运行的Win7在这里,因此,如果发生这种情况在其他操作系统的不知道)在注册表中设置超时较高,但没有按T似乎是一个理想的解决方案。

In our callback we quickly post to another thread, so that probably isn't the issue. I've read about solutions in Windows 7 for setting the timeout higher in the registry because Windows 7 is more aggressive about the timeouts apparently (we're all running Win7 here, so not sure if this occurs on other OS's) , but that doesn't seem like an ideal solution.

我已经考虑只是有一个后台线程运行一段时间,这是hackish的刷新钩每一次,但我不知道的这样做的任何真正的消极后果,而且似乎不是改变一个全球性的Windows注册表设置好。

I've considered just having a background thread running to refresh the hook every once in a while, which is hackish, but I don't know of any real negative consequences of doing that, and it seems better than changing a global Windows registry setting.

其他任何建议或解决方案?那台钩和它们连接都是静态的代表,所以他们不应该得到GC'd 这两个类。

Any other suggestions or solutions? Both the class that sets the hooks and the delegates they are attached to are static, so they shouldn't be getting GC'd.

编辑:用电话核实 GC.Collect的(); ,他们仍然可以工作,所以他们没有得到garbaged收集

Verified with calls to GC.Collect(); that they still work, so they are not getting garbaged collected.

推荐答案

我觉得这是一个超时问题。

I think this has to be a timeout issue.

其他的开发商已经报道了Windows7的具体问题,如果他们超过一个(无证)的超时值被解开较低水平挂钩。

Other developers have reported a Windows7 specific problem with low level hooks being unhooked if they exceed an (undocumented) timeout value.

请参阅的对其他开发人员讨论同样的问题,这个线程。这可能是你需要执行一个忙循环(或一个缓慢的垃圾收集),而不是睡眠造成脱钩的行为。在LowLevelKeyboardProc功能,断点也可能会造成超时问题。 (还有的另一个任务重的CPU负载可能引起行为的观察 - 大概是因为其他任务从LowLevelKeyboardProc功能抢断CPU周期,导致它花太长的时间。)

See this thread for other developers discussing the same problem. It may be that you need to perform a busy loop (or a slow Garbage Collection) rather than a Sleep to cause the unhooking behavior. A breakpoint in the LowLevelKeyboardProc function might also create timeout problems. (There's also the observation that a heavy CPU load by another task might provoke the behavior - presumably because the other task steals CPU cycles from the LowLevelKeyboardProc function and causes it to take too long.)

该解决方案在该线程建议就是尽量设置在 LowLevelHooksTimeout HKEY_CURRENT_USER\Control Panel\Desktop 在注册表中的DWORD值更大的值。

The solution suggested in that thread is to try setting the LowLevelHooksTimeout DWORD value in the registry at HKEY_CURRENT_USER\Control Panel\Desktop to a larger value.

记住,C#的辉煌之一是,如果发生垃圾回收,甚至简单的语句可以采取过多的时间量。这(被其它线程或CPU负载)可能解释问题的间歇性

Remember that one of the glories of C# is that even simple statements can take an inordinate amount of time if a garbage collection occurs.. This (or CPU loading by other threads) might explain the intermittent nature of the problem.

这篇关于有什么可以导致Windows解开一个较低水平(全球)键盘钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆