Skip to content

Instantly share code, notes, and snippets.

@SriramSakthivel
Created April 18, 2015 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SriramSakthivel/2488faffc44bd99eb79c to your computer and use it in GitHub Desktop.
Save SriramSakthivel/2488faffc44bd99eb79c to your computer and use it in GitHub Desktop.
Extension method to await ShowDialog
using System;
using System.Threading.Tasks;
using System.Windows;
namespace AsyncShowDialog
{
public static class AsyncWindowExtension
{
public static Task<bool?> ShowDialogAsync(this Window self)
{
if (self == null) throw new ArgumentNullException("self");
TaskCompletionSource<bool?> completion = new TaskCompletionSource<bool?>();
self.Dispatcher.BeginInvoke(new Action(() => completion.SetResult(self.ShowDialog())));
return completion.Task;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment