locked
ready() and Access Denied RRS feed

  • Question

  • I have added two dialogue pop ups to a page.  One in the onactivated function and one in an onready function.

    The weird thing that I cannot explain is that the one inside the onactivated function throws an "access denied" error when the onready function exists.  Comment either of them out and the other works fine.

    I can see they are conflicting but for my own development I would really like to understand the exact reason for this conflict.  I can kind of guess but I wonder if someone more experienced could please explain what is happening here to me?

    Here is the code:

    (function () {
        "use strict";
    
        WinJS.Binding.optimizeBindingReferences = true;
    
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
    
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // When the onready function is added this method call gets an access denied error
                   alert("onlaunch","message");
                } else {
                    // Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll());
            }
        };
    
        app.start();
    
        function alert(m1,m2) {
            var messageDialog = new Windows.UI.Popups.MessageDialog(m1,m2).showAsync();
        } 
    
        WinJS.Utilities.ready(function () {
            alert("onready", "message");
        }, true).done();
    
    })();


    Tuesday, February 5, 2013 3:02 AM

Answers

  • Hi Tech,

    The problem you are facing here is that you cannot stack multiple message dialogs on top of each other -- e.g you can only display one at a time.

    Please use the following alert() function define to replace yours

    (function () {
        var alertsToShow = [];
        var dialogVisible = false;
    
        function showPendingAlerts() {
            if (dialogVisible || !alertsToShow.length) {
                return;
            }
    
            dialogVisible = true;
            (new Windows.UI.Popups.MessageDialog(alertsToShow.shift())).showAsync().done(function () {
                dialogVisible = false;
                showPendingAlerts();
            })
        }
        window.alert = function (message) {
            if (window.console && window.console.log) {
                window.console.log(message);
            }
    
            alertsToShow.push(message);
            showPendingAlerts();
        }
    })();

    Here is a discussion on similar issue. please check the following link for more information

    http://stackoverflow.com/questions/13652413/what-is-the-alternative-to-alert-in-metro-apps/13655351#13655351

    Hope this helps, thanks.


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, February 6, 2013 6:06 AM
    Moderator

All replies

  • It seems that if an alert is up and another alert tries to send it throws access denied error.  I guess this is due to the fact it is an async function and I have not strung them together with a .then().  I guess I need to make some sort of message buffer but how escapes me right now. 

    What would be the best way to handle alerts coming from separate events in different parts of the code but have a chance of firing at the same time?

    Tuesday, February 5, 2013 12:57 PM
  • Hi Tech,

    The problem you are facing here is that you cannot stack multiple message dialogs on top of each other -- e.g you can only display one at a time.

    Please use the following alert() function define to replace yours

    (function () {
        var alertsToShow = [];
        var dialogVisible = false;
    
        function showPendingAlerts() {
            if (dialogVisible || !alertsToShow.length) {
                return;
            }
    
            dialogVisible = true;
            (new Windows.UI.Popups.MessageDialog(alertsToShow.shift())).showAsync().done(function () {
                dialogVisible = false;
                showPendingAlerts();
            })
        }
        window.alert = function (message) {
            if (window.console && window.console.log) {
                window.console.log(message);
            }
    
            alertsToShow.push(message);
            showPendingAlerts();
        }
    })();

    Here is a discussion on similar issue. please check the following link for more information

    http://stackoverflow.com/questions/13652413/what-is-the-alternative-to-alert-in-metro-apps/13655351#13655351

    Hope this helps, thanks.


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, February 6, 2013 6:06 AM
    Moderator
  • Hi Tech Guy 8008,

    I can repro the behavior you mentioned even by using the following code to directly executing the MessageDialog popup code twice.

    // Ready event of Page Control
    ready: function (element, options) {
                // TODO: Initialize the page here.
                alert("test1", "test1");
               
                alert("test2", "test2");


            },

    by setting breakpoint, we can find that the MessageDialog has been constructed, it is after the "showAsync" call(both executed) that the error occurs. Since MessageDialog is a windows runtime class, it is likely that the underlying implementation of the async dialog displaying code need to exclusively hold on some UI or other system resources for displaying the model dialog UI. And if we execute multiple times the showAsync call, it would occur the error. To determine the exact internal error details, that will require more deep-dive debugging on the windows runtime component (might require source code access). For our current app development, we need to make sure that our code properly serialize the MessageDialog displaying code so as to ensure one dialog show is executed after the previous one is finished. And Yanping has provided a sample implementation of such kind of serialized async alert method.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, February 6, 2013 7:36 AM
    Moderator