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));
}
As a general rule, ConfigureAwait(false) should be used for every await unless the method needs its context.
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.
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.
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.
ConfigureAwait(false)
would not do anything here, because there is no await
to configure.
It's "configure await", not "configure task".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With