典型的Web实现如下..。
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
// TODO: Do work before inner handler here
// Call the inner handler.
var response = await base.SendAsync(request, cancellationToken);
// TODO: Do work _after_ inner handler here
return response;
}处理CancellationToken.IsCancellationRequested == true的首选方法是什么?
我是否应该:
发布于 2013-05-28 20:46:29
当令牌被取消时,取消语义将抛出异常(例如,CancellationToken.ThrowIfCancellationRequested)。
如果您没有任何异步工作要做(除了base.SendAsync),那么您可以忽略令牌。
注意,如果令牌被取消,await base.SendAsync可能会引发异常。异常将自然传播,但如果您有任何清除必须进行,无论取消,使用using或finally块。
https://stackoverflow.com/questions/16801078
复制相似问题