取消一个异步任务或一组任务(C# 和 Visual Basic)
如果您不希望等待应用程序结束,则可以设置可用于取消异步应用程序的按钮。 通过按照此主题中的示例,可将取消按钮添加到下载一个网站或网站列表内容的应用程序。
描述使用 微调异步应用程序(C# 和 Visual Basic) 描述的 UI。
备注
若要运行此示例,需在计算机上安装 Visual Studio 2012、Visual Studio 2013、Visual Studio Express 2012 for Windows Desktop(Visual Studio Express 2012 for Windows Desktop)、Visual Studio Express 2013 for Windows 或 .NET Framework 4.5 或 4.5.1。
取消任务
第一个示例使“取消”按钮与单个下载任务关联。 如果在应用程序下载内容时选择按钮,下载将取消。
下载示例
您可以从 Async Sample: Fine Tuning Your Application(异步示例:微调应用程序)下载完整的 Windows Presentation Foundation (WPF) 项目,然后按照这些步骤操作。
解压下载的文件,然后启动 Visual Studio。
在菜单栏上,依次选择**“文件”、“打开”和“项目/解决方案”**。
在“打开项目”对话框中,打开包含您解压缩的示例代码的文件夹,然后打开 AsyncFineTuningCS 或 AsyncFineTuningVB 的解决方案 (.sln) 文件。
在“解决方案资源管理器”中,打开“CancelATask”项目的快捷菜单,然后选择“设置为启动项目”。
选择 F5 键运行项目。
选择 Ctrl+F5 键运行项目,但不对其进行调试。
如果您不希望下载该项目,可以在本主题结尾查看 MainWindow.xaml.vb 和 MainWindow.xaml.cs 文件。
生成示例
下面的更改会将“取消”按钮添加到下载网站的应用程序。 如果不希望下载或生成该示例,则可以在此主题结束时在“完成示例”部分检查最终产品。 星号标记出代码中的更改。
要自己生成示例,请按照“下载示例”部分的说明一步一步操作,但请选择“StarterCode”作为“启动项目”而不是“CancelATask”。
然后向该项目的 MainWindow.xaml.vb 或 MainWindow.xaml.cs 文件添加以下更改。
声明 CancellationTokenSource 变量,cts,这在所有可对其进行访问的方法范围内。
Class MainWindow ' ***Declare a System.Threading.CancellationTokenSource. Dim cts As CancellationTokenSource
public partial class MainWindow : Window { // ***Declare a System.Threading.CancellationTokenSource. CancellationTokenSource cts;
将下列事件处理程序添加为“取消”按钮。 当用户请求取消时,事件处理程序使用 CancellationTokenSource.Cancel 方法通知 cts。
' ***Add an event handler for the Cancel button. Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs) If cts IsNot Nothing Then cts.Cancel() End If End Sub
// ***Add an event handler for the Cancel button. private void cancelButton_Click(object sender, RoutedEventArgs e) { if (cts != null) { cts.Cancel(); } }
对“开始”按钮的事件处理程序作出以下改动,startButton_Click。
实例化 CancellationTokenSource、cts。
' ***Instantiate the CancellationTokenSource. cts = New CancellationTokenSource()
// ***Instantiate the CancellationTokenSource. cts = new CancellationTokenSource();
在调用 AccessTheWebAsync(下载指定网站的内容)的过程中,将 cts 的 CancellationTokenSource.Token 属性当做一个参数进行发送。 如果请求取消,则 Token 属性会传播消息。 如果用户选择取消下载操作,则添加显示消息的 catch 块。 下面的代码显示变更。
Try ' ***Send a token to carry the message if cancellation is requested. Dim contentLength As Integer = Await AccessTheWebAsync(cts.Token) resultsTextBox.Text &= String.Format(vbCrLf & "Length of the downloaded string: {0}." & vbCrLf, contentLength) ' *** If cancellation is requested, an OperationCanceledException results. Catch ex As OperationCanceledException resultsTextBox.Text &= vbCrLf & "Download canceled." & vbCrLf Catch ex As Exception resultsTextBox.Text &= vbCrLf & "Download failed." & vbCrLf End Try
try { // ***Send a token to carry the message if cancellation is requested. int contentLength = await AccessTheWebAsync(cts.Token); resultsTextBox.Text += String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength); } // *** If cancellation is requested, an OperationCanceledException results. catch (OperationCanceledException) { resultsTextBox.Text += "\r\nDownload canceled.\r\n"; } catch (Exception) { resultsTextBox.Text += "\r\nDownload failed.\r\n"; }
在 AccessTheWebAsync 中,请使用 HttpClient 类型的 GetAsync 方法的 HttpClient.GetAsync(String, CancellationToken) 重载来下载网页的内容。 传递 ct、AccessTheWebAsync 的 CancellationToken 参数作为第二参数。 如果该用户选择“取消”按钮,则该标记将传播消息。
下面的代码显示 AccessTheWebAsync 的变更。
' ***Provide a parameter for the CancellationToken. Async Function AccessTheWebAsync(ct As CancellationToken) As Task(Of Integer) Dim client As HttpClient = New HttpClient() resultsTextBox.Text &= String.Format(vbCrLf & "Ready to download." & vbCrLf) ' You might need to slow things down to have a chance to cancel. Await Task.Delay(250) ' GetAsync returns a Task(Of HttpResponseMessage). ' ***The ct argument carries the message if the Cancel button is chosen. Dim response As HttpResponseMessage = Await client.GetAsync("https://msdn.microsoft.com/en-us/library/dd470362.aspx", ct) ' Retrieve the website contents from the HttpResponseMessage. Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync() ' The result of the method is the length of the downloaded website. Return urlContents.Length End Function
// ***Provide a parameter for the CancellationToken. async Task<int> AccessTheWebAsync(CancellationToken ct) { HttpClient client = new HttpClient(); resultsTextBox.Text += String.Format("\r\nReady to download.\r\n"); // You might need to slow things down to have a chance to cancel. await Task.Delay(250); // GetAsync returns a Task<HttpResponseMessage>. // ***The ct argument carries the message if the Cancel button is chosen. HttpResponseMessage response = await client.GetAsync("https://msdn.microsoft.com/en-us/library/dd470362.aspx", ct); // Retrieve the website contents from the HttpResponseMessage. byte[] urlContents = await response.Content.ReadAsByteArrayAsync(); // The result of the method is the length of the downloaded website. return urlContents.Length; }
如果不取消程序,它将生成以下输出。
Ready to download. Length of the downloaded string: 158125.
如果在程序完成下载内容之前选择“取消”按钮,则程序会生成以下输出。
Ready to download. Download canceled.
取消任务列表
您可以通过将同一 CancellationTokenSource 实例与每个任务关联来扩展前面的示例以便取消许多任务。 如果选择“取消”按钮,则会取消所有未完成的任务。
下载示例
您可以从 Async Sample: Fine Tuning Your Application(异步示例:微调应用程序)下载完整的 Windows Presentation Foundation (WPF) 项目,然后按照这些步骤操作。
解压下载的文件,然后启动 Visual Studio 2012。
在菜单栏上,依次选择**“文件”、“打开”和“项目/解决方案”**。
在“打开项目”对话框中,打开包含您解压缩的示例代码的文件夹,然后打开 AsyncFineTuningCS 或 AsyncFineTuningVB 的解决方案 (.sln) 文件。
在“解决方案资源管理器”中,打开“CancelAListOfTasks”项目的快捷菜单,然后选择“设置为启动项目”。
选择 F5 键运行项目。
选择 Ctrl+F5 键运行项目,但不对其进行调试。
如果您不希望下载该项目,可以在本主题结尾查看 MainWindow.xaml.vb 和 MainWindow.xaml.cs 文件。
生成示例
要自己扩展示例,请按照“下载示例”部分的说明一步一步操作,但请选择“CancelATask”作为“启动项目”。 添加以下更改到该项目。 星号标记出程序中的更改。
添加方法以创建 Web 地址列表。
' ***Add a method that creates a list of web addresses. Private Function SetUpURLList() As List(Of String) Dim urls = New List(Of String) From { "https://msdn.microsoft.com", "https://msdn.microsoft.com/en-us/library/hh290138.aspx", "https://msdn.microsoft.com/en-us/library/hh290140.aspx", "https://msdn.microsoft.com/en-us/library/dd470362.aspx", "https://msdn.microsoft.com/en-us/library/aa578028.aspx", "https://msdn.microsoft.com/en-us/library/ms404677.aspx", "https://msdn.microsoft.com/en-us/library/ff730837.aspx" } Return urls End Function
// ***Add a method that creates a list of web addresses. private List<string> SetUpURLList() { List<string> urls = new List<string> { "https://msdn.microsoft.com", "https://msdn.microsoft.com/en-us/library/hh290138.aspx", "https://msdn.microsoft.com/en-us/library/hh290140.aspx", "https://msdn.microsoft.com/en-us/library/dd470362.aspx", "https://msdn.microsoft.com/en-us/library/aa578028.aspx", "https://msdn.microsoft.com/en-us/library/ms404677.aspx", "https://msdn.microsoft.com/en-us/library/ff730837.aspx" }; return urls; }
调用 AccessTheWebAsync 中的方法。
' ***Call SetUpURLList to make a list of web addresses. Dim urlList As List(Of String) = SetUpURLList()
// ***Call SetUpURLList to make a list of web addresses. List<string> urlList = SetUpURLList();
添加 AccessTheWebAsync 中的以下循环以处理该列表中的每个 Web 地址。
' ***Add a loop to process the list of web addresses. For Each url In urlList ' GetAsync returns a Task(Of HttpResponseMessage). ' Argument ct carries the message if the Cancel button is chosen. ' ***Note that the Cancel button can cancel all remaining downloads. Dim response As HttpResponseMessage = Await client.GetAsync(url, ct) ' Retrieve the website contents from the HttpResponseMessage. Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync() resultsTextBox.Text &= String.Format(vbCrLf & "Length of the downloaded string: {0}." & vbCrLf, urlContents.Length) Next
// ***Add a loop to process the list of web addresses. foreach (var url in urlList) { // GetAsync returns a Task<HttpResponseMessage>. // Argument ct carries the message if the Cancel button is chosen. // ***Note that the Cancel button can cancel all remaining downloads. HttpResponseMessage response = await client.GetAsync(url, ct); // Retrieve the website contents from the HttpResponseMessage. byte[] urlContents = await response.Content.ReadAsByteArrayAsync(); resultsTextBox.Text += String.Format("\r\nLength of the downloaded string: {0}.\r\n", urlContents.Length); }
由于 AccessTheWebAsync 显示长度,所以此方法不需要返回任何内容。 移除返回语句,并将方法的返回类型更改为 Task 而不是 Task。
Async Function AccessTheWebAsync(ct As CancellationToken) As Task
async Task AccessTheWebAsync(CancellationToken ct)
使用语句(而不是表达式)从 startButton_Click 调用方法
Await AccessTheWebAsync(cts.Token)
await AccessTheWebAsync(cts.Token);
如果不取消程序,它将生成以下输出。
Length of the downloaded string: 35939. Length of the downloaded string: 237682. Length of the downloaded string: 128607. Length of the downloaded string: 158124. Length of the downloaded string: 204890. Length of the downloaded string: 175488. Length of the downloaded string: 145790. Downloads complete.
如果在下载完成前选择“取消”按钮,则输出包括在取消前完成的下载的长度。
Length of the downloaded string: 35939. Length of the downloaded string: 237682. Length of the downloaded string: 128607. Downloads canceled.
完成示例
以下各节包含之前每个示例的代码。 请注意,您必须为 System.Net.Http 添加引用。
您可以从 Async 示例:优化应用程序中下载项目。
取消任务示例
下面的代码是取消单个任务的示例的完整 MainWindow.xaml.vb 或 MainWindow.xaml.cs 文件。
' Add an Imports directive and a reference for System.Net.Http.
Imports System.Net.Http
' Add the following Imports directive for System.Threading.
Imports System.Threading
Class MainWindow
' ***Declare a System.Threading.CancellationTokenSource.
Dim cts As CancellationTokenSource
Private Async Sub startButton_Click(sender As Object, e As RoutedEventArgs)
' ***Instantiate the CancellationTokenSource.
cts = New CancellationTokenSource()
resultsTextBox.Clear()
Try
' ***Send a token to carry the message if cancellation is requested.
Dim contentLength As Integer = Await AccessTheWebAsync(cts.Token)
resultsTextBox.Text &=
String.Format(vbCrLf & "Length of the downloaded string: {0}." & vbCrLf, contentLength)
' *** If cancellation is requested, an OperationCanceledException results.
Catch ex As OperationCanceledException
resultsTextBox.Text &= vbCrLf & "Download canceled." & vbCrLf
Catch ex As Exception
resultsTextBox.Text &= vbCrLf & "Download failed." & vbCrLf
End Try
' ***Set the CancellationTokenSource to Nothing when the download is complete.
cts = Nothing
End Sub
' ***Add an event handler for the Cancel button.
Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
If cts IsNot Nothing Then
cts.Cancel()
End If
End Sub
' ***Provide a parameter for the CancellationToken.
Async Function AccessTheWebAsync(ct As CancellationToken) As Task(Of Integer)
Dim client As HttpClient = New HttpClient()
resultsTextBox.Text &=
String.Format(vbCrLf & "Ready to download." & vbCrLf)
' You might need to slow things down to have a chance to cancel.
Await Task.Delay(250)
' GetAsync returns a Task(Of HttpResponseMessage).
' ***The ct argument carries the message if the Cancel button is chosen.
Dim response As HttpResponseMessage = Await client.GetAsync("https://msdn.microsoft.com/en-us/library/dd470362.aspx", ct)
' Retrieve the website contents from the HttpResponseMessage.
Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync()
' The result of the method is the length of the downloaded website.
Return urlContents.Length
End Function
End Class
' Output for a successful download:
' Ready to download.
' Length of the downloaded string: 158125.
' Or, if you cancel:
' Ready to download.
' Download canceled.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// Add a using directive and a reference for System.Net.Http.
using System.Net.Http;
// Add the following using directive for System.Threading.
using System.Threading;
namespace CancelATask
{
public partial class MainWindow : Window
{
// ***Declare a System.Threading.CancellationTokenSource.
CancellationTokenSource cts;
public MainWindow()
{
InitializeComponent();
}
private async void startButton_Click(object sender, RoutedEventArgs e)
{
// ***Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource();
resultsTextBox.Clear();
try
{
// ***Send a token to carry the message if cancellation is requested.
int contentLength = await AccessTheWebAsync(cts.Token);
resultsTextBox.Text +=
String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
}
// *** If cancellation is requested, an OperationCanceledException results.
catch (OperationCanceledException)
{
resultsTextBox.Text += "\r\nDownload canceled.\r\n";
}
catch (Exception)
{
resultsTextBox.Text += "\r\nDownload failed.\r\n";
}
// ***Set the CancellationTokenSource to null when the download is complete.
cts = null;
}
// ***Add an event handler for the Cancel button.
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
if (cts != null)
{
cts.Cancel();
}
}
// ***Provide a parameter for the CancellationToken.
async Task<int> AccessTheWebAsync(CancellationToken ct)
{
HttpClient client = new HttpClient();
resultsTextBox.Text +=
String.Format("\r\nReady to download.\r\n");
// You might need to slow things down to have a chance to cancel.
await Task.Delay(250);
// GetAsync returns a Task<HttpResponseMessage>.
// ***The ct argument carries the message if the Cancel button is chosen.
HttpResponseMessage response = await client.GetAsync("https://msdn.microsoft.com/en-us/library/dd470362.aspx", ct);
// Retrieve the website contents from the HttpResponseMessage.
byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
// The result of the method is the length of the downloaded website.
return urlContents.Length;
}
}
// Output for a successful download:
// Ready to download.
// Length of the downloaded string: 158125.
// Or, if you cancel:
// Ready to download.
// Download canceled.
}
取消任务列表示例
下面的代码是取消任务列表的示例的完整 MainWindow.xaml.vb 或 MainWindow.xaml.cs 文件。
' Add an Imports directive and a reference for System.Net.Http.
Imports System.Net.Http
' Add the following Imports directive for System.Threading.
Imports System.Threading
Class MainWindow
' Declare a System.Threading.CancellationTokenSource.
Dim cts As CancellationTokenSource
Private Async Sub startButton_Click(sender As Object, e As RoutedEventArgs)
' Instantiate the CancellationTokenSource.
cts = New CancellationTokenSource()
resultsTextBox.Clear()
Try
' ***AccessTheWebAsync returns a Task, not a Task(Of Integer).
Await AccessTheWebAsync(cts.Token)
' ***Small change in the display lines.
resultsTextBox.Text &= vbCrLf & "Downloads complete."
Catch ex As OperationCanceledException
resultsTextBox.Text &= vbCrLf & "Downloads canceled." & vbCrLf
Catch ex As Exception
resultsTextBox.Text &= vbCrLf & "Downloads failed." & vbCrLf
End Try
' Set the CancellationTokenSource to Nothing when the download is complete.
cts = Nothing
End Sub
' Add an event handler for the Cancel button.
Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
If cts IsNot Nothing Then
cts.Cancel()
End If
End Sub
' Provide a parameter for the CancellationToken.
' ***Change the return type to Task because the method has no return statement.
Async Function AccessTheWebAsync(ct As CancellationToken) As Task
Dim client As HttpClient = New HttpClient()
' ***Call SetUpURLList to make a list of web addresses.
Dim urlList As List(Of String) = SetUpURLList()
' ***Add a loop to process the list of web addresses.
For Each url In urlList
' GetAsync returns a Task(Of HttpResponseMessage).
' Argument ct carries the message if the Cancel button is chosen.
' ***Note that the Cancel button can cancel all remaining downloads.
Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)
' Retrieve the website contents from the HttpResponseMessage.
Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync()
resultsTextBox.Text &=
String.Format(vbCrLf & "Length of the downloaded string: {0}." & vbCrLf, urlContents.Length)
Next
End Function
' ***Add a method that creates a list of web addresses.
Private Function SetUpURLList() As List(Of String)
Dim urls = New List(Of String) From
{
"https://msdn.microsoft.com",
"https://msdn.microsoft.com/en-us/library/hh290138.aspx",
"https://msdn.microsoft.com/en-us/library/hh290140.aspx",
"https://msdn.microsoft.com/en-us/library/dd470362.aspx",
"https://msdn.microsoft.com/en-us/library/aa578028.aspx",
"https://msdn.microsoft.com/en-us/library/ms404677.aspx",
"https://msdn.microsoft.com/en-us/library/ff730837.aspx"
}
Return urls
End Function
End Class
' Output if you do not choose to cancel:
' Length of the downloaded string: 35939.
' Length of the downloaded string: 237682.
' Length of the downloaded string: 128607.
' Length of the downloaded string: 158124.
' Length of the downloaded string: 204890.
' Length of the downloaded string: 175488.
' Length of the downloaded string: 145790.
' Downloads complete.
' Sample output if you choose to cancel:
' Length of the downloaded string: 35939.
' Length of the downloaded string: 237682.
' Length of the downloaded string: 128607.
' Downloads canceled.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// Add a using directive and a reference for System.Net.Http.
using System.Net.Http;
// Add the following using directive for System.Threading.
using System.Threading;
namespace CancelAListOfTasks
{
public partial class MainWindow : Window
{
// Declare a System.Threading.CancellationTokenSource.
CancellationTokenSource cts;
public MainWindow()
{
InitializeComponent();
}
private async void startButton_Click(object sender, RoutedEventArgs e)
{
// Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource();
resultsTextBox.Clear();
try
{
await AccessTheWebAsync(cts.Token);
// ***Small change in the display lines.
resultsTextBox.Text += "\r\nDownloads complete.";
}
catch (OperationCanceledException)
{
resultsTextBox.Text += "\r\nDownloads canceled.";
}
catch (Exception)
{
resultsTextBox.Text += "\r\nDownloads failed.";
}
// Set the CancellationTokenSource to null when the download is complete.
cts = null;
}
// Add an event handler for the Cancel button.
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
if (cts != null)
{
cts.Cancel();
}
}
// Provide a parameter for the CancellationToken.
// ***Change the return type to Task because the method has no return statement.
async Task AccessTheWebAsync(CancellationToken ct)
{
// Declare an HttpClient object.
HttpClient client = new HttpClient();
// ***Call SetUpURLList to make a list of web addresses.
List<string> urlList = SetUpURLList();
// ***Add a loop to process the list of web addresses.
foreach (var url in urlList)
{
// GetAsync returns a Task<HttpResponseMessage>.
// Argument ct carries the message if the Cancel button is chosen.
// ***Note that the Cancel button can cancel all remaining downloads.
HttpResponseMessage response = await client.GetAsync(url, ct);
// Retrieve the website contents from the HttpResponseMessage.
byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
resultsTextBox.Text +=
String.Format("\r\nLength of the downloaded string: {0}.\r\n", urlContents.Length);
}
}
// ***Add a method that creates a list of web addresses.
private List<string> SetUpURLList()
{
List<string> urls = new List<string>
{
"https://msdn.microsoft.com",
"https://msdn.microsoft.com/en-us/library/hh290138.aspx",
"https://msdn.microsoft.com/en-us/library/hh290140.aspx",
"https://msdn.microsoft.com/en-us/library/dd470362.aspx",
"https://msdn.microsoft.com/en-us/library/aa578028.aspx",
"https://msdn.microsoft.com/en-us/library/ms404677.aspx",
"https://msdn.microsoft.com/en-us/library/ff730837.aspx"
};
return urls;
}
}
// Output if you do not choose to cancel:
//Length of the downloaded string: 35939.
//Length of the downloaded string: 237682.
//Length of the downloaded string: 128607.
//Length of the downloaded string: 158124.
//Length of the downloaded string: 204890.
//Length of the downloaded string: 175488.
//Length of the downloaded string: 145790.
//Downloads complete.
// Sample output if you choose to cancel:
//Length of the downloaded string: 35939.
//Length of the downloaded string: 237682.
//Length of the downloaded string: 128607.
//Downloads canceled.
}
请参见
参考
概念
使用 Async 和 Await 的异步编程(C# 和 Visual Basic)