Saturday, January 28, 2023

Why should you avoid making async void methods? Make the methods instead return Task.

 Why should you avoid making async void methods? Make the methods instead return Task.

  • 1. Processes are always crashed by exceptions thrown by async void methods.
  • 2. Even the option to wait for the result is not available to callers.
  • 3. The caller cannot report exceptions to telemetry.
  • 4. If your VS package was started in this manner, it is unable to be responsibly blocked in Package.Close until your async work is complete.
  • 5. Use caution: when passing an async delegate or async () => become async void methods when passed to a method that accepts Action delegates. If a method accepts Func<Task> or Func<Task<T>> parameters, only pass async delegates to those methods.

No comments:

Post a Comment

A Deep Dive into Computed Columns in Entity Framework Core

Entity Framework Core (EF Core) is a popular Object-Relational Mapping (ORM) framework that simplifies database access for .NET applications...