Advice on using the Dispatcher Priority and Binding
In my application I'm using the idle-time of the UI thread to offload expensive operations as described by MSDN article on the WPF Threading Model.
GenerateDataAction = () => { GenerateData(); };
Dispatcher.BeginInvoke(GenerateDataAction, DispatcherPrio开发者_如何学编程rity.Render, null);
In the GenerateDate()
method I access an MSSQL database, process the data in, and update bindings on the viewmodel. I have noticed since implementing this that some binding fail to update properly or not at all. I have check the output for binding errors and had a second programmer confirm the logic, also have set breakpoints within the dependency property changed method (the breakpoints do not get hit).
Is there any best-practice advice on which DispatcherPriority
(link to MSDN) should be used when the invoked action contains bindings?
A very good article about WPF dispatcher: http://weblogs.asp.net/pawanmishra/archive/2010/06/06/understanding-dispatcher-in-wpf.aspx
As a WPF programmer, we can push our custom time consuming logic into the queue maintained by the
Dispatcher
class and associate a lower priority value to that work item. Based on the value of priority field the corresponding code will be executed at the specified interval. Important thing to note here is that all the work is still being done by the UIthread, its just that with the help ofDispatcherPriority
we have prioritized our tasks. Ideally its recommended to give priority values less then 7(Render
) to the custom logic that we wish to execute with the help of Dispatcher. Most often priority valueBackground
is used for application specific custom logic. MS Word spell check is implemented with the help of this mechanism and priority value isApplicationIdle
.
精彩评论