0

The ShowAsync() method for MessageDialog is sporadically failing. It's pretty much a coin flip as to whether it works or not:

private async Task CloseApp()
{
    MessageDialog restartMessage = new MessageDialog("Changes have made a restart necessary.", "App must Restart")
    restartMessage.Commands.Add(new UICommand("Close Application", (command) => { Application.Current.Exit(); }));

    await restartMessage.ShowAsync(); // Code breaks here
    Application.Current.Exit();
}

I found another user with an almost identical problem, but every solution on that page fails to prevent my error from occurring. Their solution looks something like this:

private async Task CloseApp()
{
    IAsyncOperation<IUICommand> asyncCommand = null;
    MessageDialog restartMessage = new MessageDialog("Changes have made a restart necessary.", "App must Restart")
    restartMessage.Commands.Add(new UICommand("Close Application", (command) => { Application.Current.Exit(); }));
    restartMessage.DefaultCommandIndex = 0;

    asyncCommand = restartMessage.ShowAsync(); // Code *still* breaks here
    Application.Current.Exit();
}

Update:

The problem may be coming from trying to run ShowAsync() on a MessageDialog in a method called by another MessageDialog. You can't have two MessageDialogs displayed at the same time, so it throws an error.

My solution that uses Dispatchers... actually still doesn't work but have a gander anyway:

MessageDialog restartMessage = new MessageDialog("Changes have made a restart necessary.", "App must Restart");
restartMessage.Commands.Add(new UICommand("Close Application", (command) => { Application.Current.Exit(); }));

CoreDispatcher cD = Window.Current.CoreWindow.Dispatcher;
await cD.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
    await restartMessage.ShowAsync();
});
Community
  • 1
  • 1
TS-
  • 317
  • 3
  • 15
  • why not trying to display the second message dialog after you close the first one ? I guess that you need the result of the first one to know if you have to display the second one. – Vincent Jul 07 '16 at 12:05
  • I thought that the CoreDispather would cause the second message to be opened after the first one finishes... but apparently it doesn't. For the record the only thing the first message dialog does it call my SaveDate() method. This piece of code I'm using is called inside of the SaveData() method, when something goes wrong and I need the application to close. I could make this work if there were a way to force close any open MessageDialogs before putting up my restartMessage, but I don't know of any way to do that. – TS- Jul 07 '16 at 12:15

1 Answers1

0

Once I figured out that the problem was from opening a MessageDialog from within a MessageDialog, I was able to use the solution implemented here:

MessageDialog ShowAsync throws accessdenied exception on second dialog

Community
  • 1
  • 1
TS-
  • 317
  • 3
  • 15