As far as I know, there are three ways to call async method. (I'm sure there are much more)
Task.Run(async () = {...}).
AAA();
and AAA should be looks like
async void AAA()
{
...
}
Device.InvokeMainThread(async () => {...});
I know If I use option 1(Task.run
) process will go on background thread and option 3 is not.
What about option 2? It's on main thread if I called from OnAppearing()
?
First of all, Is there not any problem to use like that?
And option2 and 3 are same?
A Task returning async method can be awaited, and when the task completes, the continuation of the task is scheduled to run. A void returning async method cannot be awaited; it is a "fire and forget" method. It does work asynchronously, and you have no way of telling when it is done.
Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.
With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started. Figure 2 illustrates that exceptions thrown from async void methods can't be caught naturally.
Android support async and await - this document provides explanations and an example of using the new syntax with Xamarin. Xamarin's Async support is built on the Mono 3.0 foundation and upgrades the API profile from the being a Mobile-friendly version of Silverlight to be a mobile-friendly version of . NET 4.5.
AAA
on a thread pool thread.AAA
on the calling thread.AAA
on the main thread.As an aside, you should avoid async void methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With