locked
MessageDialog.ShowAsync and System.UnauthorizedAccessException RRS feed

  • Question

  • I use this message when my device connected successfully. When i connected first time there is no exception but when i do reconnect i have UnauthorizedAccessException on md.ShowAsync(). The problem is that MessageDialog.ShowAsync() function throws an error System.UnauthorizedAccessException.

    I saw the solution for similar problem but it does not work for me.

    async void ShowMessageBox(string msgstr, string msgtitle, Boolean bConnectiomStatus)
            {
                if (msgstr == null)
                    msgstr = "";
                if (msgtitle == null)
                    msgtitle = "";
    
                MessageDialog md = new MessageDialog(msgstr, msgtitle);
                bool? result = null;
                md.Commands.Add(new UICommand("OK", new UICommandInvokedHandler((cmd) => result = true)));
    
                await md.ShowAsync();
    
                if (result == true)
                {
                    md.ShowAsync().Cancel();
                    if (bConnectiomStatus)
                    {
                        UpdateConnectionStatus(_session.User.Name, "Connected");
                    }
                    else
                        UpdateConnectionStatus(_session.User.Name, "Not Connected");
                }
            }
    
                

    Wednesday, January 22, 2014 12:58 PM

All replies

  • it happens if ShowAsync() is called when a MessageDialog is already open. Probally your method ShowMessageBox is called twice.

    what you can do to delay the second:

    private SemaphoreSlim _semaphore = new SemaphoreSlim(1);
            async void ShowMessageBox(string msgstr, string msgtitle, Boolean bConnectiomStatus)
            {
                if (msgstr == null)
                    msgstr = "";
                if (msgtitle == null)
                    msgtitle = "";
    
                await _semaphore.WaitAsync();
    
                MessageDialog md = new MessageDialog(msgstr, msgtitle);
                bool? result = null;
                md.Commands.Add(new UICommand("OK", new UICommandInvokedHandler((cmd) => result = true)));
    
                await md.ShowAsync();
    
                if (result == true)
                {
                    md.ShowAsync().Cancel();
                    if (bConnectiomStatus)
                    {
                        UpdateConnectionStatus(_session.User.Name, "Connected");
                    }
                    else
                        UpdateConnectionStatus(_session.User.Name, "Not Connected");
                }
                _semaphore.Release();
            }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, January 22, 2014 1:12 PM
  • Unfortunately, it did not work. Is there opportunity to close md.ShowAsync if it is open? 
    Wednesday, January 22, 2014 5:05 PM