locked
Windows.UI.Popups.MessageDialog + Access denied error

    Question

  • Hi,

    I am trying to do a simple post from my app. If the post succeeds I show the message to user and if it fails I show the issue message to user. I use  Windows.UI.Popups.MessageDialog for showing message to user.

    if the POST fails, I want to give my user an option to try again or cancel. The issue is that when the user clicks the try again twice I get an "Access is denied" error.

    Below is my code. Please let me know what I am doing wrong here ?

    - Girija

    function saveData() {
        UIFunctions.toggleVisibility(progressDots, true);
        WinJS.xhr({
            type: "POST", url: "http://<server>:<port>/api/<name>",
            headers: { "Content-type": "application/json" },
            data: JSON.stringify({ A: "njksanc", B: "ncksnck" })
        }).done(
                function (xhr) {
                    var info = JSON.parse(xhr.response);
                    if (info) {
                        showMessageSave(true);
                    }
                    else {
                        showMessageSave(false);
                    }
                },
                function (error) {
                    showMessageSave(false);
                }
            );
    }
    
    // TODO Girija : Check for the solution
    function showMessageSave(success) {
        popupCompleted = false;
        if (success) {
            var message = "Settings are sucessfully saved";
            // Create the message dialog and set its content
            var msg = new Windows.UI.Popups.MessageDialog(message, "Success");
    
            // Add commands and set their command handlers
            msg.commands.append(new Windows.UI.Popups.UICommand("OK", commandInvokedHandler, 1));
    
            // Set the command that will be invoked by default
            msg.defaultCommandIndex = 0;
    
            // Set the command to be invoked when escape is pressed
            msg.cancelCommandIndex = 1;
    
            // Show the message dialog
            msg.showAsync();
        }
        else {
            var message = "Error occured while saving";
            // Create the message dialog and set its content
            var msg = new Windows.UI.Popups.MessageDialog(message, "Error");
    
            // Add commands and set their command handlers
            msg.commands.append(new Windows.UI.Popups.UICommand("Try again", commandInvokedHandler, 2));
            msg.commands.append(new Windows.UI.Popups.UICommand("Cancel", commandInvokedHandler, 1));
    
            // Set the command that will be invoked by default
            msg.defaultCommandIndex = 0;
    
            // Set the command to be invoked when escape is pressed
            msg.cancelCommandIndex = 1;
    
            // Show the message dialog
            msg.showAsync();
        }
    }
    
    function commandInvokedHandler(command) {
        if (command.id == 2) {
            saveData();
        }
    }

    Friday, November 30, 2012 11:04 PM

Answers

  • As mentioned above, you can only have a single instance of Windows.UI.Popups.MessageDialog calling the .showAsync() function a one time. You need to manage your alerts. Something like :

    (function () {
        'use strict';
        var alert = new Windows.UI.Popups.MessageDialog('');
        var currentAlert = null;
        var que = [];
    
        var showNext = function () {
            if (currentAlert !== null) return;
            if (que.length > 0) {
                currentAlert = que.shift();
                alert.content = currentAlert.msg;
                alert.title = currentAlert.title;
                alert.showAsync().done(
                        function () {
                            currentAlert = null;
                            showNext();
                        },
                        function () {
                            currentAlert = null;
                            showNext();
                        },
                        function () {
                            currentAlert = null;
                            showNext();
                        }
                    );
            }
        }
    
        var showSync = function (msg, title) {
            que.push({ msg: msg || '', title: title || '' });
            showNext();
        }
        
        var members = {
            showSync: showSync
        }
        WinJS.Namespace.define('myAlert', members);
    
    })();
    Now this is just something off the top of my head but the basic Idea is that you need to manage your alerts as to not create and show a Windows.UI.Popups.MessageDialog while one is already showing. So now you can :
    for (var i = 0; i < 5; i++) {
       myAlert.showSync('msg #' + i);
     }
     myAlert.showSync('Last Message');

    This is by no means a complete solution but you get the Idea.
    • Marked as answer by Girija Beuria Wednesday, February 20, 2013 9:06 PM
    Wednesday, February 20, 2013 6:23 PM
  • look (with tracing) if showMessageSave is called multiple times. any other MessageDialogs maybe visible from other errors?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Girija Beuria Wednesday, February 20, 2013 9:06 PM
    Monday, December 3, 2012 12:33 PM
  • Full disclosure that I wrote the library, but if you check out Eight.js at http://eight.codeplex.com the library handles this for you. You can show messages and it will queue them for you -- pretty much exactly like Bernard's implementation above, but with a more performant queue implementation.


    • Edited by got.dibbs Wednesday, February 20, 2013 8:53 PM
    • Marked as answer by Girija Beuria Wednesday, February 20, 2013 9:06 PM
    Wednesday, February 20, 2013 8:48 PM

All replies

  • Hi,

    If you try to show a message box while there is already another message box on the screen, you will get the "Access is denied" error.

    Hope this will help

    Alex


    woodhead is as woodhead does

    Saturday, December 1, 2012 1:29 AM
  • Hi Alex,

    I hope I am not doing that. The other message box is invoked after the first one closes.

    Do I do that in the code above ? What is the change I need to do ?

    - Girija

    Saturday, December 1, 2012 10:15 AM
  • Hi,

    Using try catch block for showAsync();

    Monday, December 3, 2012 1:20 AM
  • Hi,

    I tried using try.. catch.. Did not help.

    - Girija

    Monday, December 3, 2012 6:25 AM
  • look (with tracing) if showMessageSave is called multiple times. any other MessageDialogs maybe visible from other errors?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Girija Beuria Wednesday, February 20, 2013 9:06 PM
    Monday, December 3, 2012 12:33 PM
  • As mentioned above, you can only have a single instance of Windows.UI.Popups.MessageDialog calling the .showAsync() function a one time. You need to manage your alerts. Something like :

    (function () {
        'use strict';
        var alert = new Windows.UI.Popups.MessageDialog('');
        var currentAlert = null;
        var que = [];
    
        var showNext = function () {
            if (currentAlert !== null) return;
            if (que.length > 0) {
                currentAlert = que.shift();
                alert.content = currentAlert.msg;
                alert.title = currentAlert.title;
                alert.showAsync().done(
                        function () {
                            currentAlert = null;
                            showNext();
                        },
                        function () {
                            currentAlert = null;
                            showNext();
                        },
                        function () {
                            currentAlert = null;
                            showNext();
                        }
                    );
            }
        }
    
        var showSync = function (msg, title) {
            que.push({ msg: msg || '', title: title || '' });
            showNext();
        }
        
        var members = {
            showSync: showSync
        }
        WinJS.Namespace.define('myAlert', members);
    
    })();
    Now this is just something off the top of my head but the basic Idea is that you need to manage your alerts as to not create and show a Windows.UI.Popups.MessageDialog while one is already showing. So now you can :
    for (var i = 0; i < 5; i++) {
       myAlert.showSync('msg #' + i);
     }
     myAlert.showSync('Last Message');

    This is by no means a complete solution but you get the Idea.
    • Marked as answer by Girija Beuria Wednesday, February 20, 2013 9:06 PM
    Wednesday, February 20, 2013 6:23 PM
  • Full disclosure that I wrote the library, but if you check out Eight.js at http://eight.codeplex.com the library handles this for you. You can show messages and it will queue them for you -- pretty much exactly like Bernard's implementation above, but with a more performant queue implementation.


    • Edited by got.dibbs Wednesday, February 20, 2013 8:53 PM
    • Marked as answer by Girija Beuria Wednesday, February 20, 2013 9:06 PM
    Wednesday, February 20, 2013 8:48 PM