简体   繁体   中英

Fire and forget without async void

I have three methods where the first result will be used in the next two methods and no data expected to be return back.

 result= await DataAccess.Query(param); //Query 
    await DataAccess.Create(result);
    await DataAccess.Update(result);
  1. Do I really need to use await here?

  2. is it correct to use async void in create and update function?

  3. what will be the right approach to do a fire and forget here?
  4. if Im not mentioning async will it be fire and forget?
  5. what is the significance of async without await if it only used to run synchronously? I can even achieve that without that keyword.

no data expected to be return back.

What about errors? Do you need to return an error code if an error occurs, or is 200 OK acceptable even if the Create or Update fails?

I'm assuming you'll need an error code. 99.99% of calls do.

Do I really need to use await here?

Well, if you want synchronous methods, then you can just call synchronous APIs. I don't know why you would want that, though.

As a reminder: await has nothing to do with returning to the browser. It has everything to do with using fewer thread pool threads, allowing your server to scale further.

is it correct to use async void in create and update function?

No. Never.

what will be the right approach to do a fire and forget here?

The correct approach is "don't". Fire-and-forget is difficult to do correctly, and in fact since you need an error code, you can't do fire-and-forget.

I write more about fire-and-forget - including why StartNew and Task.Run are invalid solutions - on my blog . Note that the only fully reliable solution (including upgrade scenarios) is the last one (distributed architecture).

what is the significance of async without await if it only used to run synchronously? I can even achieve that without that keyword.

It's running serially (in order), not synchronously (blocking a thread). The benefit of async is to allow greater scalability. For more information, see my intro to async on ASP.NET article.

If you need to call several methods in specific order, but this whole set of functions can run asynchronously, I would do:

Task.Run(() =>
{
    result = Function();
    Create(result);
    Update(result);
});

This will separate group of functions into new thread. Since you are not awaiting this task, it is fire and forget.

If your functions are defined as async, you can wait for them to finish like this:

Task.Run(() =>
{
    var task = Function();
    task.Wait();
    var result = task.Result;

    Create(result).Wait();
    Update(result).Wait();
});

But when you are not going to benefit from async methods, it will be better to override your methods to run synchronously and use the first code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM