Which one the correct Fire and Forget?

winanjaya 146 Reputation points
2023-11-05T01:42:43.39+00:00

Hi All,

I still confuse about async and await, I have a big and long process and I don't want to wait that process to finish.

which one of the following C# code is correct to do the Fire and Forget?

  • BigAndLongProcessAsync(); // just call the method without async?, but my understanding if without await it would become synchronus
  • Task.Run(async () => await BigAndLongProcessAsync()); // create a task to call async method with await

or

  • Task.Run(async () => BigAndLongProcessAsync()); // create task to call async method without await
internal async Task BigAndLongProcessAsync()
{ 
    // some big and long process with await inside
    // send email notification of each result of the process
}

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2023-11-05T01:58:09.8333333+00:00

    A few things:

    1. You don't need the async on the method signature if there's no await in the body, so you can drop it from this:

    Task.Run(async () => BigAndLongProcessAsync());

    1. Calling BigAndLongProcessAsync(); without an await will result in all the synchronous code that's executed in BigAndLogProcessAsync to be run synchronously on the calling thread. If there's an await in this method, everything after that line will be executed asynchronously, in a fire-and-forget manner, on a new thread.
    2. Calling your method in a Task.Run allows you to run the entire method (including the synchronous code that precedes the first await inside your method) on a separate thread.

    Whether you use Task.Run depends on whether you want to offload the synchronous work to the separate thread immediately. This may not be ideal as that synchronous code may need to run on the main thread as that thread is the only one that has access to certain resources (e.g. the main thread in Winforms is the thread that you'll want to manipulate UI elements from), in which case your first example would be necessary.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. SurferOnWww 4,721 Reputation points
    2023-11-05T02:59:02.7766667+00:00

    First of all, please note that the purpose of the async programming in the ASP.NET is to increase scalability unlike Windows desktop app. For details, see the following Microsoft document:

    Async Programming : Introduction to Async/Await on ASP.NET

    Second, please note that the ASP.NET app itself is async app as described in the above document.

    Therefore, a use of Task.Run is one of things you have to avoid in the ASP.NET programming. For details, see the following Microsoft document:

    ASP.NET Core Best Practices

    As described on the above document, if BigAndLongProcessAsync() calls data access, I/O, and long-running operations APIs asynchronously, make controller/Razor Page actions asynchronous so that the entire call stack is asynchronous in order to benefit from async/await patterns.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.