Isilmë O.
Isilmë O.

Reputation: 1746

Using "await" in Dispatcher.RunAsync() throws an exception

Relevant code is as follows:

Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
    MessageDialog dialog = new MessageDialog("Wrong username or passwork. Please try again.");
    await dialog.ShowAsync();
    this.LoginButton.IsEnabled = true;
});

When I run this, E_ACCESSDENIED is thrown.

Is async-await here allowed?

Upvotes: 1

Views: 3294

Answers (1)

Servy
Servy

Reputation: 203830

Dispatcher.RunAsync is not designed to take an async delegate. It is designed to itself return a Task so that it can be awaited. The method that you give it should be non-async method.

The actual signature of the delegate it accepts is public delegate void DispatchedHandler()

Because the delegate is void returning RunAsync will think that it's finished as soon as it awaits for the first time, rather than when it's actually done. This means that whatever code is awaiting this method is continuing on well before it should.

Upvotes: 5

Related Questions