Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure.Await(false) with fire and forget async calls

Currently in my console application, I do the following to delete files in fire and forget style.

I was wondering if there is any performance gain in setting ConfigureAwait(false) for each of these Task.Run calls?

(My assumption is no, since I am not awaiting the call but I am not sure)

 for(var file in files)
  '
  '
  // Check for certain file condition and decide to delete it. 
  '
  '
  if(shouldDeleteFile)
  {
    Task.Run(() => File.Delete(file));
  }
like image 718
NavidM Avatar asked Feb 22 '19 23:02

NavidM


People also ask

Is it OK to use ConfigureAwait false only on the first await in my method and not on the rest?

As a general rule, ConfigureAwait(false) should be used for every await unless the method needs its context.

What is configure await false?

Calling ConfigureAwait(false) after the task means that we do not care if the code after the await, runs on the captured context or not. In the output console, “True” will be printed since the synchronization context is not kept.

What is ConfigureAwait () used for?

ConfigureAwait(continueOnCapturedContext: false) is used to avoid forcing the callback to be invoked on the original context or scheduler. This has a few benefits: Improving performance.

Is ConfigureAwait false needed in .NET core?

NET Core you won't need to spread ConfigureAwait(false) all over your code. Almost! This is almost true, it is still recommended the utilization of ConfigureAwait(false) for libraries as a fallback if those libraries are used within a legacy framework.


2 Answers

ConfigureAwait(false) would not do anything here, because there is no await to configure.

It's "configure await", not "configure task".

like image 186
Stephen Cleary Avatar answered Nov 14 '22 23:11

Stephen Cleary


It is self-explanatory if you look at the signature of the method itself:

public ConfiguredTaskAwaitable ConfigureAwait (bool continueOnCapturedContext);

the argument is continueOnCapturedContext, it is a continuation but you said you are doing your job in fire and forget manner, you are not doing await. Conclusion, there is no effect since you do not have any continuation.

like image 35
Johnny Avatar answered Nov 15 '22 00:11

Johnny