我应该将 CancellationToken 传递给 Task.Run 吗?

Should I pass a CancellationToken to Task.Run?

关于这个例子:

return Task.Run(
   async () => {
     while (!stoppingToken.IsCancellationRequested) {
       await Task.Delay(1000);
      }
    }, stoppingToken);

我是否应该将 stoppingToken 传递给 Task.Run?这如何改变代码行为?

如果任务计划 运行 但尚未开始,如果在任务实际开始之前请求取消,传递令牌将阻止任务开始。

the docs:

A cancellation token that can be used to cancel the work if it has not yet started.

请注意,在您的示例中,您也可以将令牌传递给 Task.Delay,以最大限度地减少响应取消请求所花费的时间。