我的创建示例例如调用链接使用异步使用Web客户端,现在等待的方法我要附加取消异步调用的功能也。 但我不能够得到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呼叫没有取消。 为什么取消按钮无法取消异步调用?