Dispatcher
Dispatcher is just like a threadpool in C# or can be simply said as message queue. It is associated with UI Thread. Any action you made on the UI, will place an entry into the dispatcher queue. E.g. UI screen changes, a call to UI method in the code behind etc.
Each entry in the dispatcher queue will executed synchronously. Generally we do not use dispatcher explicitly in a single threaded application. where as if we have some background threads to perform some operations, then dispatcher would be useful.
E.g. If we are uploading larger file to the data store, UI will hang till it gets uploaded since by default all calls are synchronous. If we need to make a responsive UI, then uploading operation needs to be performed asyncronously. In this case, dispatcher will be useful, just like below.
public
MainWindow()
{
InitializeComponent();
Task.Factory.StartNew(() =>
{
BeginInvokeExample();
});
}
private
void
BeginInvokeExample()
{
DispatcherOperation op = Dispatcher.BeginInvoke((Action)(() => {
btn1.Content =
"By BeginInvoke"
;
}));
}
Comments
Post a Comment