![](/img/trans.png)
[英]Global low-level mouse hook doesn't get called when UWP desktop app is out of focus
[英]install low-level mouse hook in different thread
我需要在WPF應用程序中安裝WH_KEYBOARD_LL掛鈎,這本身並不是一個大問題。 但是,我需要將其安裝在我的主應用程序線程之外的其他線程中,因為否則,當UI線程繁忙時,光標將凍結。 我已經閱讀了這篇文章和這個 SO問題,但是還不能完全解決。 問題中的注釋建議我在創建鈎子的線程中啟動一個Application,但是隨后我得到異常提示說我無法在同一AppDomain中啟動2個應用程序。 是否有解決方案,或者有另一種更簡單的方法將鈎子安裝到另一個線程中?
您可以使用以下代碼通過WPF調度程序創建新線程:
public class DispatcherBuilder : IBuilder<Dispatcher>
{
public Dispatcher Build()
{
Dispatcher dispatcher = null;
var manualResetEvent = new ManualResetEvent(false);
var thread = new Thread(() =>
{
dispatcher = Dispatcher.CurrentDispatcher;
var synchronizationContext = new DispatcherSynchronizationContext(dispatcher);
SynchronizationContext.SetSynchronizationContext(synchronizationContext);
manualResetEvent.Set();
Dispatcher.Run();
});
thread.Start();
manualResetEvent.WaitOne();
manualResetEvent.Dispose();
return dispatcher;
}
}
Build
方法使用WPF Dispatcher創建一個新線程,並在其上更正同步上下文。 該線程一直運行,直到您關閉調度程序為止。 然后,您可以使用例如Dispatcher.BeginInvoke
創建您的鈎子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.