惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - I'm CY

Graphviz(02) 汉字subgraph不显示汉字的处理 Graphviz(01) notepad++ Run xxx.gv How to design a product table for many kinds of product where each product has many parameters MVVM ICommand.CanExecute parameter is null 使用ContentPresenter,不使用ContentControl Code First ef SQL Server 版本不支持数据类型“datetime2” 'System.ValueTuple, Version=0.0.0.0 required for Add-Migration on .NET 4.6.1 Class Library LDAP 相关链接 .NET WCF Return String 字符串有反斜杠的处理 word2010多级列表编号为什么会变成黑块 C# javascript 采用 RSA 加密解密 [LINK]List of .NET Dependency Injection Containers (IOC) 转:javascript判断IE浏览器 How to:Aborting a long running task in TPL Exception has been thrown by the target of an invocation C#、IronPython、PowerShell 简单设计 大杀器 Linq手动添加EntitySet关系需要注意的地方
How to: Cancel a Task and Its Children
I'm CY · 2013-08-27 · via 博客园 - I'm CY

http://msdn.microsoft.com/en-us/library/dd537607.aspx

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var tokenSource = new CancellationTokenSource();
      var token = tokenSource.Token;

      // Store references to the tasks so that we can wait on them and   
      // observe their status after cancellation. 
      Task t;
      var tasks = new ConcurrentBag<Task>();

      Console.WriteLine("Press any key to begin tasks...");
      Console.WriteLine("To terminate the example, press 'c' to cancel and exit...");
      Console.ReadKey();
      Console.WriteLine();

      // Request cancellation of a single task when the token source is canceled.  
      // Pass the token to the user delegate, and also to the task so it can   
      // handle the exception correctly.
      t = Task.Factory.StartNew( () => DoSomeWork(1, token), token);
      Console.WriteLine("Task {0} executing", t.Id);
      tasks.Add(t);

      // Request cancellation of a task and its children. Note the token is passed  
      // to (1) the user delegate and (2) as the second argument to StartNew, so   
      // that the task instance can correctly handle the OperationCanceledException.
      t = Task.Factory.StartNew( () => {
                                     // Create some cancelable child tasks.  
                                     Task tc;
                                     for (int i = 3; i <= 10; i++) {
                                        // For each child task, pass the same token  
                                        // to each user delegate and to StartNew.
                                        tc = Task.Factory.StartNew( iteration => DoSomeWork((int)iteration, token), i, token);
                                        Console.WriteLine("Task {0} executing", tc.Id);
                                        tasks.Add(tc);
                                        // Pass the same token again to do work on the parent task.   
                                        // All will be signaled by the call to tokenSource.Cancel below.
                                        DoSomeWork(2, token);
                                     } 
                                  }, 
                                  token);

        Console.WriteLine("Task {0} executing", t.Id);
        tasks.Add(t);

        // Request cancellation from the UI thread.  
        if (Console.ReadKey().KeyChar == 'c') {
            tokenSource.Cancel();
            Console.WriteLine("\nTask cancellation requested.");

            // Optional: Observe the change in the Status property on the task.  
            // It is not necessary to wait on tasks that have canceled. However,  
            // if you do wait, you must enclose the call in a try-catch block to  
            // catch the TaskCanceledExceptions that are thrown. If you do   
            // not wait, no exception is thrown if the token that was passed to the   
            // StartNew method is the same token that requested the cancellation. 
        } 

        try {
            Task.WaitAll(tasks.ToArray());
        }
        catch (AggregateException e) {
           Console.WriteLine("\nAggregateException thrown with the following inner exceptions:");
           // Display information about each exception.  
           foreach (var v in e.InnerExceptions) {
              if (v is TaskCanceledException)
                 Console.WriteLine("   TaskCanceledException: Task {0}", 
                                   ((TaskCanceledException) v).Task.Id);
              else
                 Console.WriteLine("   Exception: {0}", v.GetType().Name);
           } 
           Console.WriteLine();
        } 

        // Display status of all tasks.  
        foreach (var task in tasks)
            Console.WriteLine("Task {0} status is now {1}", task.Id, task.Status);
   }

   static void DoSomeWork(int taskNum, CancellationToken ct)
   {
      // Was cancellation already requested?  
      if (ct.IsCancellationRequested == true) {
         Console.WriteLine("Task {0} was cancelled before it got started.",
                           taskNum);
         ct.ThrowIfCancellationRequested();
      } 

      int maxIterations = 100;

      // NOTE!!! A "TaskCanceledException was unhandled  
      // by user code" error will be raised here if "Just My Code" 
      // is enabled on your computer. On Express editions JMC is  
      // enabled and cannot be disabled. The exception is benign.  
      // Just press F5 to continue executing your code.  
      for (int i = 0; i <= maxIterations; i++) {
         // Do a bit of work. Not too much.  
         var sw = new SpinWait();
         for (int j = 0; j <= 100; j++)
            sw.SpinOnce();

         if (ct.IsCancellationRequested) {
            Console.WriteLine("Task {0} cancelled", taskNum);
            ct.ThrowIfCancellationRequested();
         } 
      } 
   } 
}  
// The example displays output like the following: 
//       Press any key to begin tasks... 
//    To terminate the example, press 'c' to cancel and exit... 
//     
//    Task 1 executing 
//    Task 2 executing 
//    Task 3 executing 
//    Task 4 executing 
//    Task 5 executing 
//    Task 6 executing 
//    Task 7 executing 
//    Task 8 executing 
//    c 
//    Task cancellation requested. 
//    Task 2 cancelled 
//    Task 7 cancelled 
//     
//    AggregateException thrown with the following inner exceptions: 
//       TaskCanceledException: Task 2 
//       TaskCanceledException: Task 8 
//       TaskCanceledException: Task 7 
//     
//    Task 2 status is now Canceled 
//    Task 1 status is now RanToCompletion 
//    Task 8 status is now Canceled 
//    Task 7 status is now Canceled 
//    Task 6 status is now RanToCompletion 
//    Task 5 status is now RanToCompletion 
//    Task 4 status is now RanToCompletion 
//    Task 3 status is now RanToCompletion