What's the difference between InvokeAsync and BeginInvoke for WPF Dispatcher

前端 未结 4 873
庸人自扰
庸人自扰 2020-12-08 13:09

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

4条回答
  •  一个人的身影
    2020-12-08 13:37

    There is a difference in method signature:

    BeginInvoke(Delegate, Object[])
    InvokeAsync(Action)
    

    For BeginInvoke() compiler creates array Object[] implicitly while for InvokeAsync() such array is not needed:

    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)
    

提交回复
热议问题