diiN__________
diiN__________

Reputation: 7676

Error when using await with MessageDialog.ShowAsync()

Inside a method I have a try catch block like this:

try
{
    // do something
}
catch (Exception ex)
{
    MessageDialog message = new MessageDialog(ex.ToString());
    message.ShowAsync();
}

I get the following warning for the line message.ShowAsync():

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Said and done:

try
{
    // do something
}
catch (Exception ex)
{
    MessageDialog message = new MessageDialog(ex.ToString());
    await message.ShowAsync();
}

Now I get an exception:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

I even tried this to avoid the awaiting inside the catch block:

Exception exception;
try
{
    // do something
}
catch (Exception ex)
{
    exception = ex;
}

if (exception != null)
{
    MessageDialog message = new MessageDialog(ex.ToString());
    message.ShowAsync();
}

However, this doesn't change anything.

What do I have to do to be able to use await in this case? MessageDialog.ShowAsync() is as far as IntelliSense shows an awaitable method that returns a Windows.Foundation.IAsyncOperation<IUICommand>.

Upvotes: 3

Views: 1650

Answers (1)

MicroScripter
MicroScripter

Reputation: 148

Your Function has to be "async" to use the async operator:

ex:

 private async void Test()
 {
    MessageDialog message = new MessageDialog(ex.ToString());
    await message.ShowAsync();
 }

Upvotes: 3

Related Questions