Search code examples
c#asynchronous.net-coreasync-awaittaskdialog

Navigate TaskDialogPage after asynchronous call


I need to navigate a TaskDialogPage to another after an asynchronous operation has completed. Here's my code

TaskDialogPage page = new()
{
    //...
}

TaskDialogPage completedPage = new()
{
    //...
}

page.Created += async (_, _) =>
{
    await DoSomethingThatTakesTime().ConfigureAwait(false);
    page.Navigate(completedPage); // NotSupportedException - Illegal thread call 
};

I could use Control.Invoke() method :

page.Invoke(() => page.Navigate(completedPage))

But TaskDialogPage doesn't inherit from Control !


Solution

  • await DoSomethingThatTakesTime().ConfigureAwait(true);
    

    Got it. ConfigureAwait specifies wether there should be an attempt to get back to the original context, i.e. , the UI context. Even though ConfigureAwait(true) is the default and specified implicitly, I think this better captures the intent.