Sometimes you need to present the user with data and wait for a response all while not lock up the UI thread. Microsoft’s asynchronous programming model our best friend and in just a few lines we can wait for a response from DialogBuilder. In this code example we’ll wait for AlertDialog’s positive or negative button being tapped.
This AlertAsync method uses the TaskCompletionSource<TResult> type to create the task and handle the completion. Upon the user tapping the positive or negative button we call TrySetResult() on the TaskCompletionSource to return our bool value.