Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>.

like image 879
diiN__________ Avatar asked Nov 28 '25 08:11

diiN__________


1 Answers

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();
 }
like image 57
MicroScripter Avatar answered Nov 29 '25 21:11

MicroScripter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!