C # async / wait with / without wait (fire and forget)

I have the following code:

static async Task Callee() { await Task.Delay(1000); } static async Task Caller() { Callee(); // #1 fire and forget await Callee(); // #2 >1s Task.Run(() => Callee()); // #3 fire and forget await Task.Run(() => Callee()); // #4 >1s Task.Run(async () => await Callee()); // #5 fire and forget await Task.Run(async () => await Callee()); // #6 >1s } static void Main(string[] args) { var stopWatch = new Stopwatch(); stopWatch.Start(); Caller().Wait(); stopWatch.Stop(); Console.WriteLine($"Elapsed: {stopWatch.ElapsedMilliseconds}"); Console.ReadKey(); } 

# 1 is triggered and forgotten in the easiest way. # 2 is just waiting. Interesting material begins with number 3. What is the deep logic of calls?

I know about using fire'n'forget warnings in ASP.NET, as stated here . I ask this because we move our application to the service fabric, where we can no longer use HostingEnvironment.QueueBackgroundWorkItem(async cancellationToken => await LongMethodAsync()); , and the advice is to simply replace it with Task.Run .

I see Task.Run starting a new thread, what will be the difference between # 3 and # 5?

-2
source share
2 answers

I ask about this because we move our application to the service fabric, where we can no longer use HostingEnvironment.QueueBackgroundWorkItem (async cancelationToken => wait for LongMethodAsync ()); and the advice is to simply replace it with Task.Run.

Bad advice. You must use a separate background process, separated from your web interface by a queue .

What is the deep logic of calls?

  • Runs an asynchronous method for the current thread. Ignores all results (including exceptions).
  • Runs an asynchronous method for the current thread. Asynchronously awaiting completion. This is the standard way to call asynchronous code.
  • Runs an asynchronous method in a thread pool thread. Ignores all results (including exceptions).
  • Runs an asynchronous method in a thread pool thread. Asynchronously awaiting completion.
  • Just like # 3.
  • Just like # 4.
+7
source

"26." Fire and forget "ok if you never forget." Maxim 26 .

If you make any fire and forget the script, you have a huge risk of swallowing the Exception. Swallowing any exception - but especially a fatal one - is a deadly sin in handling exceptions. All that you finish is a program in memory that will create even less understandable and reproducible exceptions. So never start. Here are two interesting articles to read on the mater:

http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in -NET

Full on Threads are known to be capable of swallowing exceptions. Indeed, you need to do the work so as not to assimilate them, and then check to see if they had one after they finished. You should have at least some sort of follow-up task that logs (and possible exposure) to exceptions. Or do you really regret that "fire and forget."

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1274266/


All Articles