Issue
If I have this code:
protected override async void OnAppearing()
{
base.OnAppearing();
await DoLengthDatabaseTaskAsync(); // takes 30 seconds
}
Will the fact that it is an Async task mean that the screen appears quickly and does not wait on the database task to complete?
protected override async void OnAppearing()
{
base.OnAppearing();
await CallAndForgetAboutOutputTaskAsync(); // takes 30 seconds
}
How about if I am not concerned "call and forget" about the completion of the CallAndForgetAboutOutputTaskAsync() method.
Is there a way I can run CallAndForgetAboutOutputTaskAsync() in the background so the screen appears as soon as possible
Solution
Yes and no.
OnAppearing
means that the page has appeared on the screen and it performs immediately after that. So executing something there won't slow down anything.
However you should keep in mind that OnAppearing
is executing on the UI thread and depending on the structure of your CallAndForgetAboutOutputTaskAsync()
it may freeze your UI, but simple magic of Task.Run
resolves the problem.
EDIT: So it should be like:
Task CallAndForget()
{
return Task.Run(async() => await action());
}
Answered By - Ivan Ičin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.