问题描述
我注意到,在.NET 4.5的已经得到了一套新的方法到Dispatcher的线程调用上执行的东西。之前,.NET 4.5,我们有Invoke和它处理了这个syncronously和异步分别。
除了命名和可用的略有不同的重载,还有的的BeginInvoke
和 InvokeAsync
之间的主要区别方法?
哦,我已经签,既可以是的await
编辑:
专用异步任务RunStuffOnUiThread(动作动作)
{
//这两个作品的精
等待dispatcher.BeginInvoke(动作);
等待dispatcher.InvokeAsync(动作);
}
有没有差异的的BeginInvoke
方法调用私有 LegacyBeginInvokeImpl
方法,它itslef调用私有方法 InvokeAsyncImpl
(由 InvokeAsync使用的方法
)。因此,它基本上是同样的事情。看起来这是一个简单的重构,但它奇怪的的BeginInvoke
方法并没有被标记为过时。
BeginInvoke的:
公开的DispatcherOperation的BeginInvoke(优先级的DispatcherPriority,代表法)
{
返回this.LegacyBeginInvokeImpl(优先级,方法,空,0);
}私人的DispatcherOperation LegacyBeginInvokeImpl(优先级的DispatcherPriority,代表法,对象ARGS,INT numArgs)
{
Dispatcher.ValidatePriority(优先优先权);
如果(方法== NULL)
{
抛出新的ArgumentNullException(办法);
}
的DispatcherOperation的DispatcherOperation =新的DispatcherOperation(这一点,方法,优先级,指定参数时,numArgs);
this.InvokeAsyncImpl(的DispatcherOperation,CancellationToken.None);
返回的DispatcherOperation;
}
InvokeAsync:
公开的DispatcherOperation InvokeAsync(动作回调的DispatcherPriority优先)
{
返回this.InvokeAsync(回调,优先级,CancellationToken.None);
}公众的DispatcherOperation InvokeAsync(动作回调的DispatcherPriority优先的CancellationToken的CancellationToken)
{
如果(回调== NULL)
{
抛出新的ArgumentNullException(回调);
}
Dispatcher.ValidatePriority(优先优先权);
的DispatcherOperation的DispatcherOperation =新的DispatcherOperation(此,优先级,回调);
this.InvokeAsyncImpl(的DispatcherOperation,的CancellationToken);
返回的DispatcherOperation;
}
I noticed in .NET 4.5 that the WPF Dispatcher had gotten a new set of methods to execute stuff on the Dispatcher's thread called InvokeAsync. Before, .NET 4.5 we had Invoke and BeginInvoke which handled this syncronously and asynchronously respectively.
Besides the naming and the slightly different overloads available, are there any major differences between the BeginInvoke
and the InvokeAsync
methods?
Oh, and I already checked, both can be await
ed:
private async Task RunStuffOnUiThread(Action action)
{
// both of these works fine
await dispatcher.BeginInvoke(action);
await dispatcher.InvokeAsync(action);
}
There are no differences as the BeginInvoke
method calls a private LegacyBeginInvokeImpl
method which itslef calls the private method InvokeAsyncImpl
(the method used by InvokeAsync
). So it's basically the same thing. It seems like it's a simple refactoring, however it's strange the BeginInvoke
methods weren't flagged as obsolete.
BeginInvoke :
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method)
{
return this.LegacyBeginInvokeImpl(priority, method, null, 0);
}
private DispatcherOperation LegacyBeginInvokeImpl(DispatcherPriority priority, Delegate method, object args, int numArgs)
{
Dispatcher.ValidatePriority(priority, "priority");
if (method == null)
{
throw new ArgumentNullException("method");
}
DispatcherOperation dispatcherOperation = new DispatcherOperation(this, method, priority, args, numArgs);
this.InvokeAsyncImpl(dispatcherOperation, CancellationToken.None);
return dispatcherOperation;
}
InvokeAsync :
public DispatcherOperation InvokeAsync(Action callback, DispatcherPriority priority)
{
return this.InvokeAsync(callback, priority, CancellationToken.None);
}
public DispatcherOperation InvokeAsync(Action callback, DispatcherPriority priority, CancellationToken cancellationToken)
{
if (callback == null)
{
throw new ArgumentNullException("callback");
}
Dispatcher.ValidatePriority(priority, "priority");
DispatcherOperation dispatcherOperation = new DispatcherOperation(this, priority, callback);
this.InvokeAsyncImpl(dispatcherOperation, cancellationToken);
return dispatcherOperation;
}
这篇关于什么是WPF的调度InvokeAsync而BeginInvoke的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!