18

我创建了一个示例示例以使用 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 调用不会被取消。为什么取消按钮无法取消异步调用?

4

3 回答 3

29

早于 .Net 4.5的异步功能WebClient,因此它仅部分支持基于任务的异步模式。这包括拥有自己的取消机制:方法CancelAsync()即使使用新-TaskAsync方法也可以。要在取消 a 时调用此方法CancellationToken,可以使用它的Register()方法

cts.Token.Register(wc.CancelAsync);

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

于 2012-12-10T23:52:53.657 回答
5

基于 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);
    }
}
于 2017-08-23T16:21:59.410 回答
4

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行实际上是同步完成的。

于 2012-12-10T13:26:15.760 回答