Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

async pitfalls

$
0
0

Take this for example (Android), a button on ContentPage:

    protected void OnButtonClick(object sender, EventArgs e)
    {
        var ignore = ShowAlertAsync();
    }

    private async Task ShowAlertAsync()
    {
        await DisplayAlert("tubo", "buuuuuuuu", "Accept", "Cancel");
    }

Now run the app, click the button and dialog will apear. So far so good. Don't click on it but rather rotate the device so the activity gets recreated (or any other sort of activity recreation). Click your button again and dialog will appear again. Click any of the two buttons. The dialog will disappear. Everything is fine until now. Now, the old dialog will appear. Click any of the two buttons and you'll get an exception:

System.InvalidOperationException: The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Cancel…

Right, one might opt to prevent activity recreation when orientation changes, but still, there are other events that can and will recreate activities.
And I fear other weird stuff might happen when using async/await (like when navigating).


Viewing all articles
Browse latest Browse all 58056

Trending Articles