0

So I have the following code (kinda) where I need to call MessageDialog.ShowAsync and wait for the result:

public object Execute()
{
   var messageDialog = new MessageDialog("", "");
   await messageDialog.ShowAsync();
}

but because Execute() is part of an Interface, I cannot change its signature to return a Task nor void. So the best I've come up with so far is this:

public object Execute()
{
   var messageDialog = new MessageDialog("", "");
   messageDialog.ShowAsync().AsTask().ContinueWith(c =>
   {
       // ...               
   });
}
svick
  • 236,525
  • 50
  • 385
  • 514
Ismael
  • 361
  • 3
  • 9
  • possible duplicate of [How would I run an async Task method synchronously?](http://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously) – N_A May 05 '14 at 19:25
  • In short: messageDialog.ShowAsync().AsTask().Wait(); – N_A May 05 '14 at 19:28
  • 7
    @mydogisbox: The top two answers on that question are extremely dangerous; [this one is better](http://stackoverflow.com/a/5110698/263693). The best solution is to change the interface; you're trying to block the UI thread, and you're not supposed to do that. – Stephen Cleary May 05 '14 at 19:42
  • 1
    @StephenCleary fair enough. The question is still a dup. ;-) – N_A May 05 '14 at 19:43
  • @cadessi, if you really cannot change the interfaces, you *might* be able to use a nested message loop like [`WaitWithNestedMessageLoop`](http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx), using `CoreDispatcher`.`ProcessEvents` / `RunAsync` / `StopProcessEvents`. This is untested and **not** recommended. – noseratio May 05 '14 at 23:21

0 Answers0