WPF外包就找北京动点飞扬软件:WPF 4.5探秘之三 Dispatcher的新方法

 这是WPF 4.5的新特性介绍系列的第三部分。

  当你开始做的事情不同步时,Dispatcher有可能是最常用到的。因为这是从另一个线程来更新UI控件唯一的方法。

  即使它很容易使用,WPF的团队也为它添加了13种方法。特别是新的await keyword。在本文里,我们将探秘这些新方法。

  新的“类”方法

  有的过载将作为一个参数Func委托。在之前的版本里,在Dispatcher的可用方法里无法返回一些东西(除了void),但是在新版本里已实现了这一功能。

  这种新方法是:

  Invoke(Func)

  Invoke(Func, DispatcherPriority)

  Invoke(Func, DispatcherPriority, CancellationToken)

  Invoke(Func, DispatcherPriority, CancellationToken, TimeSpan)

  在WPF 4.5之前,返回一些东西,这段代码应该写为:

//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

  现在,您可以使用这个编写一个更容易维持的代码:

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 !

  WPF团队已经注意到了一些新生的‘await’关键字,并且Dispatcher现在也可以支持这些关键字了。

  以下是一些新的方法:

  1. InvokeAsync(Action)

  2. InvokeAsync(Action, DispatcherPriority)

  3. InvokeAsync(Action, DispatcherPriority, CancellationToken)

  4. InvokeAsync(Func)

  5. InvokeAsync(Func, DispatcherPriority)

  6. InvokeAsync(Func, DispatcherPriority, CancellationToken)

  这些方法返回DispatcherOperation / DispatcherOperation类型的对象。然后您就可以在它本身或者是“Task”属性里使用await关键字。

  下面举个例子:

public async void CallingMethod()
{
await Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);
}

最后将会出现一个内容为“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).”的免责申明/警告。

Cancellation

最后,您可能已经注意到CancellationToken类型的新参数。这是.NET 4 Cancellation Framework的一部分。在后台中,Dispatcher操作将开始创建一个受到这个取消指令的管理Task对象。

如果Task没有启动即你的调度程序操作没有启动,那么它将不再启动。如果它已经开始那么它不会被停止,并且继续执行。

事实上,取消指令的停止与否完全取决于任务本身的运转。然而,我没有为给定的Action找到一种方法跳过Cancellation指令,而不再使用在第一段中关于返回对象提到过的相同的方法。

并没有一定要使用取消指令的场景,但是我认为这是使异步关键字的工作的必要条件。

这种情况在demo中很难复制,但是我们还找到了一个工作例子:

//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 cancelled
Console.WriteLine("The operation didn't even start !");
}

Bonus

另外还有两个更容易使用的方法:

1. Invoke(Action)

2. Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan) 

原文地址:https://www.cnblogs.com/dotfun/p/2721213.html