This is the third part of the new feature introduction series of WPF 4.5.
Dispatcher may be the most common practice when you start different steps. This is the only way to update the UI control from another thread.
Even if it is easy to use, the WPF team adds 13 Methods to it. In particular, the new await keyword. In this article, we will explore these new methods.
New "class" Method
Some overload will be delegated as a parameter func. In earlier versions, some items (except void) cannot be returned in the available methods of dispatcher, but this function has been implemented in the new version.
The new method is:
Invoke (func)
Invoke (func, dispatcherpriority)
Invoke (func, dispatcherpriority, cancellationtoken)
Invoke (func, dispatcherpriority, cancellationtoken, timespan)
Some items are returned before WPF 4.5. This code should be written as follows:
// The function which returns something (here of type object)
Func <Object> myreturningobjectfunction = () =>
{// For example only!
Return new object ();
};
Object returnedoject = NULL;
// A mock action to be abble to return the object
Action mockaction = () =>{ returnedoject = myreturningobjectfunction ();};
// Actually invoke the Method
Dispatcher. currentdispatcher. Invoke (mockaction, null );
// Here I can use the returned object
Now, you can use this to write a code that is easier to maintain:
Public void callingmethod ()
{
Object returnedoject = dispatcher. currentdispatcher
. Invoke (myreturningobjectfunction, null );
}
// This method can now live alone!
Private object myreturningobjectfunction ()
{
// For example only!
Return new object ();
}
Await-ready!
The WPF team has noticed some new 'await' keywords, and dispatcher can now support these keywords.
Here are some new methods:
1. invokeasync (Action)
2. invokeasync (action, dispatcherpriority)
3. invokeasync (action, dispatcherpriority, cancellationtoken)
4. invokeasync (func)
5. invokeasync (func, dispatcherpriority)
6. invokeasync (func, dispatcherpriority, cancellationtoken)
These methods return objects of the dispatcheroperation/dispatcheroperation type. Then you can use the await keyword in its own or "task" attribute.
The following is an example:
Public async void callingmethod ()
{
Await dispatcher. currentdispatcher. invokeasync (myreturningobjectfunction );
}
At last, a "calling task. wait will result in a deadlock if the operation is queued on a calling thread. for more information about using a task to perform asynchronous operations, see task parallelism (task parallel library ). "disclaimer/warning.
Cancellation
Finally, you may have noticed the new cancellationtoken parameter. This is part of the. NET 4 Cancellation framework. In the background, the dispatcher operation starts to create a management task object that receives the cancellation command.
If the task is not started, that is, your scheduling program operation is not started, it will not start again. If it has started, it will not be stopped and the execution will continue.
In fact, the stop of canceling the command depends entirely on the operation of the task itself. However, I did not find a way to skip the cancellation command for the given action, instead of using the same method mentioned in the first section about the returned object.
There is no need to cancel the command, but I think this is a necessary condition for making asynchronous keywords work.
This situation is difficult to copy in the demo, but we also found a working example:
// Create a token Source
VaR CTS = new cancellationtokensource ();
// Launch the cancel of the token
Task. Factory. startnew () => cts. Cancel ());
// Launch an operation with priority normal
// This will delay the execution of the following invoke call.
Dispatcher. begininvoke (new action () =>
{
Int I = 0; while (I <300)
// Update the UI to be sure to block the following
// Action. Rendering will occur and takes precedence
{I ++; _ countertextblock. Text = I. tostring ();}
}), Null );
Try
{
// Launch a task with the cancellation token
Dispatcher. Invoke () =>{ while (true );},
Dispatcherpriority. Background, CTS. Token );
}
Catch (operationcanceledexception ex)
{
// We are here because the token was canceled
Console. writeline ("the operation didn't even start! ");
}
Bonus
There are two more easy-to-use methods:
1. Invoke (Action)
2. Invoke (action, dispatcherpriority, cancellationtoken, timespan)