Open
Description
In each of the C# examples, "ComputeNextMove()" returns a Task, but is not awaited. This is bad practice, and will result in lost exceptions and other negative side effects. Both examples are also exactly the same.
Here's a corrected C# code sample:
public class AsyncExample
{
private async void NextMove_Click(object sender, RoutedEventArgs e)
{
// The await causes the handler to return immediately.
await Task.Run(async () => await ComputeNextMove());
// Or even better, just: // await ComputeNextMove();
// Now update the UI with the results.
// ...
}
private async Task ComputeNextMove()
{
// Perform background work here.
// Don't directly access UI elements from this method.
}
}
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 5ed3306f-fb9a-0be1-b372-4a073bb3e57c
- Version Independent ID: fddfcca6-819d-e48a-9ab2-d12ab56ba796
- Content: Keep the UI thread responsive - Windows UWP applications
- Content Source: windows-apps-src/debug-test-perf/keep-the-ui-thread-responsive.md
- Product: uwp
- Technology: deployment
- GitHub Login: @eliotcowley
- Microsoft Alias: elcowle