Skip to content

Improper code example #1636

Open
Open
@htxryan

Description

@htxryan

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions