是否可以安全使用ContinueWith作为"最后"操作?操作、安全、ContinueWith、QUOT

2023-09-04 10:55:14 作者:冫水嚸契约

考虑了一块code这样的:

Consider a piece of code like:

private Task<string> Download()
{
    var wc = new WebClient();
    Task<string> backgroundDownload = wc.DownloadStringTaskAsync(this.Uri);
    // Make sure the WebClient is disposed no matter what.
    backgroundDownload.ContinueWith((downloadTask) => { wc.Dispose(); });
    return backgroundDownload;
}

我可以肯定的是, WebClient.Dispose()调用发生和产生被重新抛出给调用者好像有到 ContinueWith ?

Can I be certain that the WebClient.Dispose() call occurs and that any exception produced is rethrown to the caller as if there was no call to ContinueWith?

客户端可以观察这种 ContinueWith ? (例如,稍后会调用 ContinueWith 删除处置电话吗?)

Can clients observe this ContinueWith? (e.g. will later calls to ContinueWith remove the Dispose call?)

推荐答案

随着code,你有,你可以肯定的延续而不管的code是否没有成功完成发射,是取消或引发异常。

With the code that you have you can be certain that the continuation will be fired regardless of whether or not the code completed successfully, was cancelled, or throws an exception.

在一个潜在的问题的与你的解决方案是,其他的延续可能会运行,前,中,或Web客户端配置后。如果没有与此清理之前运行的其他延续的问题上运行,然后你有什么是好的。如果这是一个问题,那么你就需要返回的延续,而不是原来的任务,但你还需要传播的结果(和例外/注销)正确。异步使用使所有的这样简单:

The one potential problem with the solution that you have is that other continuations can potentially run, before, during, or after the web client is disposed. If you don't have a problem with other continuations running before this cleanup runs then what you have is fine. If that's a problem then you'll need to return the continuation, not the original task, but you'll also need to propagate the result (and exceptions/cancellation) correctly. The use of async makes all of this way easier:

private async Task<string> Download()
{
    using(var wc = new WebClient())
      return await wc.DownloadStringTaskAsync(this.Uri);
}
 
精彩推荐
图片推荐