Jim*_*röm 6 c# task cancellation-token
我经常看到以下代码,第一个视图看起来不错,因为它用于在执行其他操作之前检查先决条件。
但是当人们读到方法的名称时,感觉前面的 if 语句已经包含在方法本身中了。那么是否有任何理由像本示例中那样编写代码,或者可以跳过 if 语句并ThrowIfCancellationRequested直接运行。
当然,如果需要在退出之前进行清理,那就是另一回事了,那么我完全理解 if 语句的用法。
if (cancellationToken.IsCancellationRequested)
{
cancellationToken.ThrowIfCancellationRequested();
}
Run Code Online (Sandbox Code Playgroud)