归根到底,也还是Windows API,即:
1.通过注册Window Message
2.给当前Window(Win32里的Window)添加Window Procedure来处理注册过的Windows Message,
3. DispatcherFrame的Push方法里则是传统的Translate和DispatchMessage的过程。直到这个Frame不再Continue.
4. 默认的Frame是由Application的RunDispatcher启动起来的,Application是DispatcherObject的子类,带有一个Dispatcher。
5. 由4上溯Application.Run则可以一直上溯到static Main()方法,程序的入口。
一些关键代码:
1. Static Dispatcher() //WindowMessage是整个操作系统级别的,会被所有WPF程序共享
Dispatcher._msgProcessQueue = UnsafeNativeMethods.RegisterWindowMessage(“DispatcherProcessQueue”);
2. Dispatcher() ctor :
this._hook = new HwndWrapperHook(this.WndProcHook);
this._window.Value.AddHook(this._hook);
—->
private IntPtr WndProcHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled):
if (msg == (int)Dispatcher._msgProcessQueue)
{
this.ProcessQueue();
}
—->
ProcessQueue():
dispatcherOperation.Invoke(); //自带ExecutionContext,顺便说一下,如果是别的线程里BeginInvoke,会ExecutionContext.Capture(),包括synchornizationContext,securityContext在内都抓下来。但是,Invoke机制对SynchronizationContext有特殊处理,这样ExecutionContext.Run()里的SetExecutionContext方法白白flow了SynchronizationContext
// System.Windows.Threading.DispatcherOperation
[SecurityCritical]
private void InvokeImpl()
{
SynchronizationContext current = SynchronizationContext.Current;
bool flag = current != this._dispatcher._dispatcherSynchronizationContext;
try
{
if (flag)
{
SynchronizationContext.SetSynchronizationContext(this._dispatcher._dispatcherSynchronizationContext);
}
this._result = this._dispatcher.WrappedInvoke(this._method, this._args, this._numArgs);
}
3.Dispatcher.PushFrameImpl()
while (frame.Continue && this.GetMessage(ref mSG, IntPtr.Zero, 0, 0))
{
this.TranslateAndDispatchMessage(ref mSG); //Translate和DispatchMessage的方法就是native的同名方法
}