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

Is it possible to call an asynchronous method from a synchronous method?

$
0
0

Hello everyone,

I'm having a Xamarin.forms application and what I'm trying to do is:
1) someone clicks on an item in a listview
2) the application shows a dialog to the user
3) the application waits until the user has pressed "yes" or "no"
4) as soon as there's input from the user, the application (code) continues again

I know that normally it's possible to make your method async and then apply an await on the DisplayAlert. However, because the code is so enormous, it has a lot of effect on the rest of my code.. So I wondered if it's possible to call an asynchronous method from a synchronous method?

To show the dialog, I use the following code:

private async Task<bool> ShowDialog()
{
return await DisplayAlert("Question?", "Waiting for user input", "Yes", "No");
}

And to await the message box I use the following the code:

public void SyncMethod()
{
// Showing the listview here
// The user clicks an item
bool result = await ShowDialogAsync();

// after the user has clicked something,
// then continue with more code here.
if (result)
// Result = true
}

Unfortunately the SyncMethod needs to be async. Is there a way to solve this without making this method async? Thanks for the answer :)


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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