【问题标题】:How to attach CancellationTokenSource to DownloadStringTaskAsync method and cancel the async call?如何将 CancellationTokenSource 附加到 DownloadStringTaskAsync 方法并取消异步调用?
【发布时间】:2012-11-27 17:22:37
【问题描述】:

我创建了一个示例示例来使用 WebClient 使用 async 和 await 方法调用链接,现在我还想附加取消异步调用功能。但我无法获取 CancellationTokenSource 令牌并将 DownloadStringTaskAsync 附加到此取消令牌。以下是我的代码,谁能告诉我如何做到这一点。

private async void DoWork()
        {
            this.Cursor = Cursors.WaitCursor;
            Write("DoWork started.");
            cts = new CancellationTokenSource();
            WebClient wc = new WebClient();
            string result = await wc.DownloadStringTaskAsync(new Uri("http://gyorgybalassy.wordpress.com"));

            if (result.Length < 100000)
            {
                Write("The result is too small, download started from second URL.");
                result = await wc.DownloadStringTaskAsync(new Uri("https://www.facebook.com/balassy"));
            }
            Write("Download completed. Downloaded bytes: " + result.Length.ToString());
            Write("DoWork ended.");
            this.Cursor = Cursors.Default;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Write("Cancellation started.");
            this.cts.Cancel();
            Write("Cancellation ended.");
        }

当我的取消按钮调用 cts.Cancel 时,DownloadStringTaskAsync 调用不会被取消。为什么取消按钮无法取消异步调用?

【问题讨论】:

  • 你没有以任何方式使用CancellationTokeSourceWebClient 怎么知道它应该在你不告诉它的情况下取消?
  • 感谢 svick 的回复。但是我尝试将令牌作为 DownloadStringTaskAsync 方法的参数传递,但该方法没有支持它的重载。因此,我没有得到如何将取消令牌与 DownloadStringTaskAsync 方法一起使用。你能给我推荐一些好书来阅读所有这些具有 TAP 功能的 C# 新更新吗?

标签: c# .net-4.5 async-await c#-5.0 cancellationtokensource


【解决方案1】:

WebClient 不支持取消。我建议您使用更新的类型,例如HttpClient

...
cts = new CancellationTokenSource();
string result;
using (var client = new HttpClient())
using (var response = await client.GetAsync("http://gyorgybalassy.wordpress.com", cts.Token))
{
  result = await response.Content.ReadAsStringAsync();
}

if (result.Length < 100000)
...

默认情况下GetAsync 方法在读取整个响应之前不会完成,因此await response.Content.ReadAsStringAsync 行实际上会同步完成。

【讨论】:

【解决方案2】:

WebClient 的异步功能早于 .Net 4.5,因此它仅部分支持 the Task-based Asynchronous Pattern。这包括拥有自己的取消机制:the CancelAsync() method,即使与新的 -TaskAsync 方法一起使用。要在取消CancellationToken 时调用此方法,可以使用its Register() method

cts.Token.Register(wc.CancelAsync);

作为替代方案,您可以使用新的 HttpClient,正如 Stephen 建议的那样,它完全支持 TAP,包括 CancellationTokens。

【讨论】:

  • 下载完成后处理cts.Token.Register返回的CancellationTokenRegistration可能不会有什么坏处。
【解决方案3】:

基于 svick 答案的扩展方法:

public static async Task<string> DownloadStringTaskAsync(this WebClient webClient, string url, CancellationToken cancellationToken) {
    using (cancellationToken.Register(webClient.CancelAsync)) {
        return await webClient.DownloadStringTaskAsync(url);
    }
}

public static async Task<string> DownloadStringTaskAsync(this WebClient webClient, Uri uri, CancellationToken cancellationToken) {
    using (cancellationToken.Register(webClient.CancelAsync)) {
        return await webClient.DownloadStringTaskAsync(uri);
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2021-12-10
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多