如何将CancellationTokenSource附加到DownloadStringTaskAsync方法并取消异步调用?

时间:2012-12-10 09:54:58

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

我创建了一个示例示例,使用async和await方法使用WebClient调用链接现在我也要附加取消异步调用功能。但我无法获得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调用。为什么取消按钮无法取消异步调用?

3 个答案:

答案 0 :(得分:19)

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

cts.Token.Register(wc.CancelAsync);

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

答案 1 :(得分:3)

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 :(得分:1)

基于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);
    }
}