06 May 2010

WPF Dispatcher

Dispatcher
Windows presentation foundation (WPF) has come up with extensive support for threading. Here I would like to share my experience with threading in WPF.Basically on what situation I have preferred to go with WPF Dispatcher with a code sample.
Define the problem:
We have a requirement in our application that need a mechanism to respond to user and system input such as mouse clicks, key presses, and system events basically having business logic in all these actions also there was a LDAP call to populate the users and cache it and we are making some WCF service call as well to the DB, these operations are going to take time and from the UI the user cannot do any operations till this complete. Not sounds good right. Yes we need to avoid this delay experience to the user while doing all these time consuming business operations.
Technical Solution:
Well , I think from the defanition it is clear that we are not going to address some application specific issue and this might be alredy addressed by any one who come across development of enterprise applications. We have addressed these issues using WPF Dispatcher.Thanks for Microsoft for introducing such a simple way of addressing this issue.
Working with Dispatcher
WPF applications start their lives with two threads: 1) the UI thread which updates the windows, controls, and visual elements of your app, and 2) the BackgroundWorker threads waiting to help you perform the heavy lifting. The ideal approach is illustrated in the very simple diagram below. The UI thread queues work items inside an object called a Dispatcher. The Dispatcher selects work items on a priority basis and runs each one to completion. Every UI thread must have at least one Dispatcher and each Dispatcher can execute work items in exactly one thread.
Basically in .net 2.0 one thread cannot access another thread’s objects directly. If a background thread does attempt to update the UI thread directly, an access violation in the form of an exception is thrown. Dispatcher provides the methods Invoke and Begin Invoke, which a background thread can use to schedule tasks on the UI thread and to specify delegates the dispatcher can use to modify the target object. Messages passed to the dispatcher are marked with a priority level and placed in a queue managed by the dispatcher.
Here I am just giving you a sample code for how UI element is getting updated using dispatcher.
public DispacherTestClass()
{
Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()DispatcherOperationdispatcher = textBox1.Dispatcher.BeginInvoke(
System.Windows.Threading.
{
textBox1.Text = "Updated Text from Thread";
}
));
dispatcher.Completed += += new EventHandler(dispatcher_Completed);
}
));
thread.Start();
}
}
void dispatcher_Completed(object sender, EventArgs e)

{
MessageBox.Show("The textbox has finished being updated!");
}
Also if we want to do a background process the code is going to be something like this:

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render , AsyncLoadCurrentStaffView, true);

And  in the delegete we need to show the method that we want to put in dispatcher que.

new AsyncLoadCurrentStaffViewDelegate(LoadCurrentStaffView);

1 comment:

Anonymous said...

Interesting blоg! Is your thеme custοm madе оr did
you downloaԁ it from somеwheгe?
A theme lіκe yоurs ωith а
fеw ѕimple twеeks would гeаllу make my blog jump οut.
Please let me know ωhеre you got youг theme.
Thаnks
Feel free to surf my web blog :: captcha bypass