简体   繁体   English

Task.Run()有什么区别,在Xamarin表单上调用异步void AAA()?

[英]What's difference Task.Run() , Calling async void AAA() on Xamarin forms?

As far as I know, there are three ways to call async method. 据我所知,有三种方法可以调用异步方法。 (I'm sure there are much more) (我敢肯定还有更多)

  • 1> Task.Run(async () = {...}). 1> Task.Run(async () = {...}).
  • 2> Just calling AAA(); 2>仅调用AAA();

and AAA should be looks like 和AAA应该看起来像

async void AAA()
{
...
}
  • 3> Device.InvokeMainThread(async () => {...}); 3> Device.InvokeMainThread(async () => {...});

I know If I use option 1( Task.run ) process will go on background thread and option 3 is not. 我知道如果我使用选项1( Task.run ),进程将在后台线程上运行,而选项3不是。

What about option 2? 那选项2呢? It's on main thread if I called from OnAppearing() ? 如果我从OnAppearing()调用,它在主线程上?
First of all, Is there not any problem to use like that? 首先,这样使用没有问题吗?

And option2 and 3 are same? 和option2和3一样吗?

  1. Executes AAA on a thread pool thread. 在线程池线程上执行AAA
  2. Executes AAA on the calling thread. 在调用线程上执行AAA
  3. Executes AAA on the main thread. 在主线程上执行AAA

As an aside, you should avoid async void methods . 顺便说一句 ,您应该避免使用异步void方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM