DispatcherObject,Dispatcher,Thread之間的關系
我們都知道WPF中的控件類都是從System.Windows.Threading.DispatcherObject繼承而來, 而DispatcherObject又在構造時與當前線程的Dispatcher關聯起來,CurrentDispatcher如果為null則會主動new一個Dispatcher并且在構造時和當前創建它的線程關聯起來了。因此整個鏈為DispatcherObject <- Dispatcher <- Thread. 具體我們一起看看反編譯的紅色代碼:
public abstract class DispatcherObject
{
? private Dispatcher _dispatcher;
? [EditorBrowsable(EditorBrowsableState.Advanced)]
? public Dispatcher Dispatcher
? {
? ? get
? ? {
? ? ? return this ._dispatcher;
? ? }
? }
? protected DispatcherObject()
? {
? ? base .\u002Ector();
? ? this ._dispatcher = Dispatcher.CurrentDispatcher;
? }
? ?........................................................
}
public sealed class Dispatcher
{
? ??public static Dispatcher CurrentDispatcher
? ?{
? ? get
? ? {
? ? ? return Dispatcher.FromThread(Thread.CurrentThread) ?? new Dispatcher();
? ? }
? ?}
? private Dispatcher()
? {
? ?.............................
? ? Dispatcher._tlsDispatcher = this ;
? ? this ._dispatcherThread = Thread.CurrentThread;
? ?.............................
? }
? ?..............................
}
這樣設計的原則就保證:界面元素只有被創建它的線程訪問
Dispatcher中Invoke,BeginInvoke和Win32中SendMessage,PostMessage的關系
上面我們提到wpf的界面元素只有被創建它的線程來訪問,如果我們想在后臺或者其他線程里該怎么辦?
答案就是利用Dispatcher的Invoke和BeginInvoke,作用就是把委托放到界面元素關聯的Dispatcher里的工作項里,然后此Dispatcher關聯的線程進行執行。
所不同的是Invoke是在關聯的線程里同步執行委托, 而BeginInvoke是在關聯的線程里異步執行委托。
做過Win32或者MFC編程的童鞋們都知道win32中有SendMessage和PostMessage類似的概念,這些概念有什么內在關系呢?
其實Dispatcher的Invoke和BeginInvoke都是調用Win32的PostMessage傳遞窗體句柄和消息號,反編譯代碼如下:
? private bool RequestForegroundProcessing()
? {
? ? if (this._postedProcessingType >= 2)
? ? ? return true;
? ? if (this._postedProcessingType == 1)
? ? ? SafeNativeMethods.KillTimer(new HandleRef((object) this, this._window.Value.Handle), 1);
? ? this._postedProcessingType = 2;
? ? return MS.Win32.UnsafeNativeMethods.TryPostMessage(new HandleRef((object) this, this._window.Value.Handle), Dispatcher._msgProcessQueue, IntPtr.Zero, IntPtr.Zero);
? }
只不過Invoke在PostMessage后調用了內部返回值DispatcherOperation的wait方法,在執行結束后才返回。反編譯代碼如下:
? ??DispatcherOperation dispatcherOperation = this.BeginInvokeImpl(priority, method, args, isSingleParameter);
? ? ? if (dispatcherOperation != null)
? ? ? {
? ? ? ? int num = (int) dispatcherOperation.Wait(timeout);
? ? ? ? if (dispatcherOperation.Status == DispatcherOperationStatus.Completed)
? ? ? ? ? obj = dispatcherOperation.Result;
? ? ? ? else if (dispatcherOperation.Status == DispatcherOperationStatus.Aborted)
? ? ? ? ? obj = (object) null;
? ? ? ? else
? ? ? ? ? dispatcherOperation.Abort();
? ? ? }
WPF的消息泵和Win32的消息泵之間的關系
WPF的窗體程序都必須隱式或者顯式調用[Application.Run()](http://msdn.microsoft.com/en-us/library/ms597010(v=vs.110).aspx)來初始化WPF窗體。當Application.Run()調用時, 會在其內部調用Dispatcher.Run()方法。最終會在PushFrame()方法內初始化消息泵。
具體為:Application.Run() -> Dispatcher.Run() -> Dispatcher.PushFrame() -> Dispatcher.PushFrameImpl()
private void PushFrameImpl(DispatcherFrame frame)
? {
? ? MSG msg = new MSG();
? ? ++this._frameDepth;
? ? try
? ? {
? ? ? SynchronizationContext current = SynchronizationContext.Current;
? ? ? bool flag = current != this._dispatcherSynchronizationContext;
? ? ? try
? ? ? {
? ? ? ? if (flag)
? ? ? ? ? SynchronizationContext.SetSynchronizationContext((SynchronizationContext) this._dispatcherSynchronizationContext);
? ? ? ? while (frame.Continue && this.GetMessage(ref msg, IntPtr.Zero, 0, 0))
? ? ? ? ? this.TranslateAndDispatchMessage(ref msg);
? ? ? ? if (this._frameDepth != 1 || !this._hasShutdownStarted)
? ? ? ? ? return;
? ? ? ? this.ShutdownImpl();
? ? ? }
? ? ? finally
? ? ? {
? ? ? ? if (flag)
? ? ? ? ? SynchronizationContext.SetSynchronizationContext(current);
? ? ? }
? ? }
? ? finally
? ? {
? ? ? --this._frameDepth;
? ? ? if (this._frameDepth == 0)
? ? ? ? this._exitAllFrames = false;
? ? }
? }
Ok,從上述反編譯的代碼可以看到,WPF還是通過Dispatcher內部實現了傳統的Win32消息循環, 如GetMessage,TranslateMessage,DispatchMessage。
下面是win32消息泵的實現, 大家可以對比下:
BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
?? if (bRet == -1)
?? {
?????? // handle the error and possibly exit
?? }
?? else
?? {
?????? TranslateMessage(&msg);
?????? DispatchMessage(&msg);
?? }
}
- 前言
- win32與WPF的混合編程
- WPF: 一個可以用StoryBoard動態改變Grid行寬/列高的類
- MFC中調用WPF教程
- Expression Blend操作: 使用behavior來控制Storyboard
- WPF DatePicker 的textbox的焦點
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事項
- WPF TreeView 后臺C#選中指定的Item, 需要遍歷
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后臺觸發 Validate UI‘s Element
- WPF ValidationRule 觸發ErrorTemplate 的注意事項
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for &#39;OnStartup&#39; matches delegate &#39;System.Windows.StartupEventHandler&#39;
- WPF error: does not contain a static &#39;Main&#39; method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 顯示選中的item
- 如何得到WPF中控件綁定的EventTrigger
- 選中DataGrid的Cell而不是row
- ContextMenu的自定義
- 輸入框只能輸入英文
- TextBox的OnTextboxChanged事件里對Text重新賦值帶中文, 導致崩潰
- DataGrid當列寬超出當前寬度時,沒有數據也恒有滾動條
- wpf如何獲取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible設為Collapse時,VisualTreeHelper.GetChildrenCount為0
- XAML 編碼規范 (思考)
- 如何為現有控件的DependencyProperty添加Value Changed事件?
- TreeView滾動TreeViewItem
- 為BindingList添加Sort
- WPF Background的設置有坑
- 自定義Panel中添加依賴屬性需要注意的問題
- TextBlock截斷字符顯示為....
- DataGrid 支持字符截斷顯示
- TreeView控件實踐
- WPF如何更改系統控件的默認高亮顏色 (Highlight brush)
- ViewModel中C# Property自動添加OnPropertyChanged處理的小工具, 以及相應Python知識點
- WPF中Xaml編譯正常而Designer Time時出錯的解決辦法
- 關于Snoop的用法
- wpf中為DataGrid添加checkbox支持多選全選
- WPF中DataGrid控件的過濾(Filter)性能分析及優化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect導致下拉框控件抖動
- 再論WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何設計歷史記錄查看UI
- WPF案例:如何設計搜索框(自定義控件的原則和方法)
- WPF基本概念入門
- WPF開發中Designer和碼農之間的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字體的設置
- Bug:DataGridCell的顯示不完整
- WPF中ToolTip的自定義
- WPF中ItemsControl綁定到Google ProtocolBuffer的結構體時的性能問題
- TreeView的性能問題
- Xaml中string(字符串)常量的定義以及空格的處理
- 依賴屬性
- WPF中的CheckBox的_ (underscore / 下劃線)丟失
- WPF錯誤:必須使“Property”具有非 null 值。
- WPF中ItemsControl應用虛擬化時找到子元素的方法
- WPF毫秒級桌面時鐘的實現-C#中Hook(鉤子)的應用
- KB2464222導致IsNonIdempotentProperty方法找不見
- WPF中PreviewMouseDownEvent的系統處理:TabItem的PreviewMouseDown 事件彈框后不切換的問題調查
- WPF文字渲染相關的問題及解決
- wpf中的默認右鍵菜單中的復制、粘貼、剪貼等沒有本地化的解決方案
- WPF內部DeliverEvent讀鎖和PrivateAddListener寫鎖導致死鎖
- Windbg調試WPF的依賴屬性
- WPF 后臺Render線程崩潰, Exception from HRESULT: 0x88980406
- WPF中DependencyObject與DependencyProperty的源碼簡單剖析
- 禁用WPF中DataGrid默認的鼠標左鍵拖動多選行的效果
- wpf工程中在Xaml文件下添加多個cs文件
- ScrollViewer滾動到底來觸發加載數據的Behavior