大网数据广告活动
.NET 与 C# 开发

在完成一个异步任务后取消剩余任务 (C#)

· 52CE 我爱测

通过结合使用 方法和 ,可在一个任务完成时取消所有剩余任务。 WhenAny 方法采用任务集合中的一个参数。 该方法启动所有任务,并返回单个任务。 当集合中任意任务完成时,完成单个任务。

通过结合使用 xref:System.Threading.Tasks.Task.WhenAny*?displayProperty=nameWithType 方法和 xref:System.Threading.CancellationToken,可在一个任务完成时取消所有剩余任务。 WhenAny 方法采用任务集合中的一个参数。 该方法启动所有任务,并返回单个任务。 当集合中任意任务完成时,完成单个任务。

此示例演示如何结合使用取消标记与 WhenAny 保留任务集合中第一个要完成的任务,并取消剩余任务。 每个任务都下载网站内容。 本示例显示第一个完成的下载的内容长度,并取消其他下载。

Note: 若要运行该示例,计算机上必须安装有 Visual Studio 2012 或更高版本和 .NET Framework 4.5 或更高版本。

下载示例

若要下载完整的 Windows Presentation Foundation (WPF) 项目,请参阅 Async Sample:Fine Tuning Your Application(异步示例:微调应用程序)。

  1. 解压缩下载的文件,然后启动 Visual Studio。

  2. 在菜单栏上,依次选择 “文件” 、 “打开” 和 “项目/解决方案” 。

  3. 在“打开项目” 对话框中,打开保存已解压的示例代码的文件夹,然后打开 AsyncFineTuningCS 的解决方案 (.sln) 文件。

  4. 在“解决方案资源管理器” 中,打开“CancelAfterOneTask” 项目的快捷菜单,然后选择“设为启动项目” 。

  5. 选择 F5 键运行该项目。

    选择 Ctrl+F5 键运行该项目,而不进行调试。

  6. 运行程序若干次,以验证首先完成的下载是不同的。

如果不想下载项目,可在本主题末尾处查看 MainWindow.xaml.cs 文件。

生成示例

本主题中的示例添加到取消异步任务或任务列表 (C#)中开发的项目,以取消任务列表。 该示例使用相同的 UI,但未显示使用“取消” 按钮。

若要自行生成示例,请按“下载示例”部分的说明逐步操作,选择“CancelAListOfTasks” 作为“启动项目” 。 将此主题中的更改添加到该项目。

在 CancelAListOfTasks 项目的 MainWindow.xaml.cs 文件中,通过将每个网站的处理步骤从 AccessTheWebAsync 中的循环移动至下列异步方法来启动转换。

// ***Bundle the processing steps for a website into one async method.  
async Task<int> ProcessURLAsync(string url, HttpClient client, CancellationToken ct)  
{  
    // GetAsync returns a Task<HttpResponseMessage>.   
    HttpResponseMessage response = await client.GetAsync(url, ct);  
  
    // Retrieve the website contents from the HttpResponseMessage.  
    byte[] urlContents = await response.Content.ReadAsByteArrayAsync();  
  
    return urlContents.Length;  
}  

在 AccessTheWebAsync 中,此示例使用查询、xref:System.Linq.Enumerable.ToArray* 方法和 WhenAny 方法创建并启动任务数组。 将 WhenAny 应用到数组将返回单个任务,该任务在等待时对任务数组中首先完成的任务进行评估。

在 AccessTheWebAsync 中,进行下列更改。 星号标记了代码文件中的更改。

  1. 注释禁止或删除循环。

  2. 创建一个查询,它在执行时将生成常规任务的集合。 每次调用 ProcessURLAsync 均在 TResult 为整数时返回 xref:System.Threading.Tasks.Task`1。

    // ***Create a query that, when executed, returns a collection of tasks.  
    IEnumerable<Task<int>> downloadTasksQuery =  
        from url in urlList select ProcessURLAsync(url, client, ct);  
    
  3. 通过调用 ToArray 来执行查询并启动任务。 下一步中应用 WhenAny 方法将在不使用 ToArray 的情况下执行查询并启动任务,但其他方法可能无法执行此操作。 最安全的做法是显式强制执行查询。

    // ***Use ToArray to execute the query and start the download tasks.   
    Task<int>[] downloadTasks = downloadTasksQuery.ToArray();  
    
  4. 在任务集合上调用 WhenAny。 WhenAny 返回 Task(Of Task(Of Integer)) 或 Task<Task<int>>。 也就是说,在等待时 WhenAny 将返回一个任务,它将评估单个的 Task(Of Integer) 或 Task<int>。 该单个任务是集合中首先完成的任务。 首先完成的任务被分配给 firstFinishedTask。 firstFinishedTask 的类型为 xref:System.Threading.Tasks.Task`1,其中 TResult 是整数,这是因为它是 ProcessURLAsync 的返回类型。

    // ***Call WhenAny and then await the result. The task that finishes   
    // first is assigned to firstFinishedTask.  
    Task<int> firstFinishedTask = await Task.WhenAny(downloadTasks);  
    
  5. 在此示例中,你只对首先完成的任务感兴趣。 因此,使用 xref:System.Threading.CancellationTokenSource.Cancel*?displayProperty=nameWithType 取消剩余任务。

    // ***Cancel the rest of the downloads. You just want the first one.  
    cts.Cancel();  
    
  6. 最后,等待 firstFinishedTask 检索下载内容的长度。

    var length = await firstFinishedTask;  
    resultsTextBox.Text += $"\r\nLength of the downloaded website:  {length}\r\n";
    

运行程序若干次,以验证首先完成的下载是不同的。

完整的示例

下列代码是示例的完整 MainWindow.xaml.cs 文件。 对添加到此示例的元素进行了星号标记。

请注意,必须为 xref:System.Net.Http 添加引用。

可以从 Async Sample:Fine Tuning Your Application(异步示例:微调应用程序)下载这些项目。

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.  
using System.Threading;  
  
namespace CancelAfterOneTask  
{  
    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);  
                resultsTextBox.Text += "\r\nDownload complete.";  
            }  
            catch (OperationCanceledException)  
            {  
                resultsTextBox.Text += "\r\nDownload canceled.";  
            }  
            catch (Exception)  
            {  
                resultsTextBox.Text += "\r\nDownload failed.";  
            }  
  
            // Set the CancellationTokenSource to null when the download is complete.  
            cts = null;  
        }  
  
        // You can still include a Cancel button if you want to.  
        private void cancelButton_Click(object sender, RoutedEventArgs e)  
        {  
            if (cts != null)  
            {  
                cts.Cancel();  
            }  
        }  
  
        // Provide a parameter for the CancellationToken.  
        async Task AccessTheWebAsync(CancellationToken ct)  
        {  
            HttpClient client = new HttpClient();  
  
            // Call SetUpURLList to make a list of web addresses.  
            List<string> urlList = SetUpURLList();  
  
            // ***Comment out or delete the loop.  
            //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 +=  
            //        $"\r\nLength of the downloaded string: {urlContents.Length}.\r\n";
            //}  
  
            // ***Create a query that, when executed, returns a collection of tasks.  
            IEnumerable<Task<int>> downloadTasksQuery =  
                from url in urlList select ProcessURLAsync(url, client, ct);  
  
            // ***Use ToArray to execute the query and start the download tasks.   
            Task<int>[] downloadTasks = downloadTasksQuery.ToArray();  
  
            // ***Call WhenAny and then await the result. The task that finishes   
            // first is assigned to firstFinishedTask.  
            Task<int> firstFinishedTask = await Task.WhenAny(downloadTasks);  
  
            // ***Cancel the rest of the downloads. You just want the first one.  
            cts.Cancel();  
  
            // ***Await the first completed task and display the results.   
            // Run the program several times to demonstrate that different  
            // websites can finish first.  
            var length = await firstFinishedTask;  
            resultsTextBox.Text += $"\r\nLength of the downloaded website:  {length}\r\n";
        }  
  
        // ***Bundle the processing steps for a website into one async method.  
        async Task<int> ProcessURLAsync(string url, HttpClient client, CancellationToken ct)  
        {  
            // GetAsync returns a Task<HttpResponseMessage>.   
            HttpResponseMessage response = await client.GetAsync(url, ct);  
  
            // Retrieve the website contents from the HttpResponseMessage.  
            byte[] urlContents = await response.Content.ReadAsByteArrayAsync();  
  
            return 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/library/hh290138.aspx",  
                "https://msdn.microsoft.com/library/hh290140.aspx",  
                "https://msdn.microsoft.com/library/dd470362.aspx",  
                "https://msdn.microsoft.com/library/aa578028.aspx",  
                "https://msdn.microsoft.com/library/ms404677.aspx",  
                "https://msdn.microsoft.com/library/ff730837.aspx"  
            };  
            return urls;  
        }  
    }  
    // Sample output:  
  
    // Length of the downloaded website:  158856  
  
    // Download complete.  
}  

请参阅

历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / .NET 中文文档历史快照
适用范围
历史中文快照 2019-08-18;详见原文产品版本;文档日期 07/20/2015
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。