2

I saw that UnauthorizedAccessException exception occurs especially when open files and not disposing them, but in my case it is different. I have a very simple code and as I think this code is correct. I am displaying a message dialog and it works fine but it may get UnauthorizedAccessException, I will explain it how. My message dialog is in a Search charm method which searches for an address in bing maps, if the address was not found, then the dialog will be shown. So this is the code (no need for the whole code, it's not affecting):

MessageDialog msg = new MessageDialog("No results found.");
await msg.ShowAsync();

If I call it once, everything is ok, but if I do the next thing, I get UnauthorizedAccessException:

  1. Open the search charm.
  2. Search for an address that does not exists (for example: sadasdasdasd).

  3. So now the message dialog will be shown with the cancel button. Now I dont press cancel, I just press again the search (with the same value).

  4. Now I get an exception while getting to this line:

    await msg.ShowAsync();
    

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Am I doing something wrong? How can I handle it?

I just want my application to be resistant to crashes.

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116

1 Answers1

2

When you call

await msg.ShowAsync();

your app is waiting, until the MessageBox is closed. So, I think you can only have one MessageBox open at the same time.

See also this post: WinRT C# MessageDialog.ShowAsync - Unauthorized Access Exception -

Community
  • 1
  • 1
  • I also think that I can have only one DialogMessage at the same time. But what can I do to prevent the user from doing this bug? He may crash my application this way... – Misha Zaslavsky Sep 08 '13 at 18:27
  • 1
    I think you are looking for this solution: http://stackoverflow.com/questions/12722490/messagedialog-showasync-throws-accessdenied-exception-on-second-dialog – Bananenbieger Sep 08 '13 at 20:20
  • 1
    Just assign the AsyncOperartion to a class variable. So if you are want to open the MessageDialog the second time, this variable isn't null and you can cancel the old MessageDialog (see the url above). – Bananenbieger Sep 08 '13 at 20:26