windows-runtimemessagedialog

MessageDialog ShowAsync throwing accessdenied exception


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();
});

Solution

  • 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