avo*_*avo 6 windows wpf winapi multithreading
对于WPF应用程序,内部是否存在经典的消息循环(在Windows中GetMessage/DispatchMessage)Application.Run?是否可以使用PostThreadMessage将另一个Win32应用程序发布的消息捕获到WPF UI线程(没有HWND句柄的消息).谢谢.
我使用 .NET Reflector 来跟踪 .NET 的Applicaton.Run实现Dispatcher.PushFrameImpl。还可以从.NET Framework 参考源获取相同的信息。确实有一个经典的消息循环:
private void PushFrameImpl(DispatcherFrame frame)
{
SynchronizationContext syncContext = null;
SynchronizationContext current = null;
MSG msg = new MSG();
this._frameDepth++;
try
{
current = SynchronizationContext.Current;
syncContext = new DispatcherSynchronizationContext(this);
SynchronizationContext.SetSynchronizationContext(syncContext);
try
{
while (frame.Continue)
{
if (!this.GetMessage(ref msg, IntPtr.Zero, 0, 0))
{
break;
}
this.TranslateAndDispatchMessage(ref msg);
}
if ((this._frameDepth == 1) && this._hasShutdownStarted)
{
this.ShutdownImpl();
}
}
finally
{
SynchronizationContext.SetSynchronizationContext(current);
}
}
finally
{
this._frameDepth--;
if (this._frameDepth == 0)
{
this._exitAllFrames = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,这是 的实现TranslateAndDispatchMessage,它确实在其内部执行过程中触发了ComponentDispatcher.ThreadFilterMessageRaiseThreadMessage事件:
private void TranslateAndDispatchMessage(ref MSG msg)
{
if (!ComponentDispatcher.RaiseThreadMessage(ref msg))
{
UnsafeNativeMethods.TranslateMessage(ref msg);
UnsafeNativeMethods.DispatchMessage(ref msg);
}
}
Run Code Online (Sandbox Code Playgroud)
显然,它适用于任何发布的消息,而不仅仅是键盘消息。您应该能够订阅ComponentDispatcher.ThreadFilterMessage并观看您感兴趣的消息。