![](/img/trans.png)
[英]What is the difference between Dispatcher.BeginInvoke and Task.Factory.StartNew
[英]What's the difference between InvokeAsync and BeginInvoke for WPF Dispatcher
我注意到在.NET 4.5中, WPF Dispatcher獲得了一組新的方法來在Dispatcher的線程上執行名為InvokeAsync的東西 。 在.NET 4.5之前,我們有Invoke和BeginInvoke ,它們分別同步和異步地處理這個。
除了可用的命名和稍微不同的重載之外, BeginInvoke
和InvokeAsync
方法之間是否存在任何重大差異?
哦,我已經檢查了,兩個都可以await
:
private async Task RunStuffOnUiThread(Action action)
{
// both of these works fine
await dispatcher.BeginInvoke(action);
await dispatcher.InvokeAsync(action);
}
沒有區別,因為BeginInvoke
方法調用私有LegacyBeginInvokeImpl
方法,該方法調用私有方法InvokeAsyncImpl
( InvokeAsync
使用的方法)。 所以它基本上是一回事。 看起來這是一個簡單的重構,但是奇怪的是BeginInvoke
方法沒有被標記為過時。
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;
}
異常處理是不同的。
您可能需要檢查以下內容:
private async void OnClick(object sender, RoutedEventArgs e)
{
Dispatcher.UnhandledException += OnUnhandledException;
try
{
await Dispatcher.BeginInvoke((Action)(Throw));
}
catch
{
// The exception is not handled here but in the unhandled exception handler.
MessageBox.Show("Catched BeginInvoke.");
}
try
{
await Dispatcher.InvokeAsync((Action)Throw);
}
catch
{
MessageBox.Show("Catched InvokeAsync.");
}
}
private void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("Catched UnhandledException");
}
private void Throw()
{
throw new Exception();
}
方法簽名有所不同:
BeginInvoke(Delegate, Object[])
InvokeAsync(Action)
對於BeginInvoke()
編譯器隱式創建數組Object[]
而對於InvokeAsync()
,則不需要這樣的數組:
IL_0001: ldarg.0
IL_0002: call instance class [WindowsBase]System.Windows.Threading.Dispatcher [WindowsBase]System.Windows.Threading.DispatcherObject::get_Dispatcher()
IL_0007: ldarg.1
IL_0008: ldc.i4.0
IL_0009: newarr [mscorlib]System.Object
IL_000e: callvirt instance class [WindowsBase]System.Windows.Threading.DispatcherOperation [WindowsBase]System.Windows.Threading.Dispatcher::BeginInvoke(class [mscorlib]System.Delegate, object[])
IL_0014: ldarg.0
IL_0015: call instance class [WindowsBase]System.Windows.Threading.Dispatcher [WindowsBase]System.Windows.Threading.DispatcherObject::get_Dispatcher()
IL_001a: ldarg.1
IL_001b: callvirt instance class [WindowsBase]System.Windows.Threading.DispatcherOperation [WindowsBase]System.Windows.Threading.Dispatcher::InvokeAsync(class [mscorlib]System.Action)
好吧,我注意到的一個區別是InvokeAsync有一個泛型重載,它返回一個DispatcherOperation作為返回值,並接受一個Func作為其委托輸入參數。 因此,您可以通過類型安全的方式通過InvokeAsync檢索操作的結果,類似於您可以等待任務的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.