Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Is it possible to wait for Device.BeginInvokeOnMainThread code to finish

$
0
0

Hello everyone,

In my code I have a task called "ShowMessageBoxAsync". I want to use this code to show (and await) the DisplayAlert to the user and return the result. Like this: var messageBoxResult = await View.ShowMessageBoxAsync("This is an error");

The code for the ShowMessageBoxAsync is:

    public async System.Threading.Tasks.Task<bool> ShowMessageBoxAsync(string message)
    {
        var result = false;
        Device.BeginInvokeOnMainThread(async () =>
        {
            result = await DisplayAlert("Error", message, "OK", "Cancel");
        });
        return result;
    }

Before I added the Device.BeginInvokeOnMainThread, the task gave me an Exception that it wasn't running on the main/UI thread. So after adding BeginInvokeOnMainThread, it started to work without exceptions. The problem, however, is that the code goes directly to the result, without waiting for the result of the "await DisplayAlert".

Is it possible return the value of "result" only after the Device.BeginInvokeOnMainThread code finishes?

Thanks in advance!


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>