Configure.Await(false) 触发并忘记异步调用

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

目前在我的控制台应用程序中,我执行以下操作以删除文件,然后忘记样式。

我想知道为每个 Task.Run 调用设置 ConfigureAwait(false) 是否有任何性能提升?

(我的假设是不,因为我不是在等待电话但我不确定)

 for(var file in files)
  '
  '
  // Check for certain file condition and decide to delete it. 
  '
  '
  if(shouldDeleteFile)
  {
    Task.Run(() => File.Delete(file));
  }

简答:否,

There’s one common kind of application that doesn’t have a SynchronizationContext: console apps. When your console application’s Main method is invoked, SynchronizationContext.Current will return null. That means that if you invoke an asynchronous method in your console app, unless you do something special, your asynchronous methods will not have thread affinity: the continuations within those asynchronous methods could end up running “anywhere.”

来源:"Await, SynchronizationContext, and Console Apps"

ConfigureAwait(false)这里不会做任何事情,因为没有await配置。

是"configure await",不是"configure task"。

如果您查看方法本身的签名,它是不言自明的:

public ConfiguredTaskAwaitable ConfigureAwait (bool continueOnCapturedContext);

论点是continueOnCapturedContext,它是一个延续,但你说你是在火上浇油地工作,你没有在做await.结论,没有效果,因为你没有任何 continuation.