• C# 任务、线程、同步(二)


    取消架构

    1、Parallel.For()方法的取消

     1  static void CancelParallelLoop()
     2         {
     3             var cts = new CancellationTokenSource();
     4             cts.Token.Register(() => Console.WriteLine("*** token canceled"));
     5 
     6             cts.CancelAfter(200);
     7 
     8             try
     9             {
    10                 ParallelLoopResult result = Parallel.For(0, 100, new ParallelOptions() { CancellationToken = cts.Token },
    11                     x => {
    12                         Console.WriteLine("loop {0} started", x);
    13                         int sum = 0;
    14                         for (int i = 0; i < 100; i++)
    15                         {
    16                             Thread.Sleep(2);
    17                             sum += i;
    18                         }
    19                         Console.WriteLine("loop {0} finished", x);
    20                     });
    21             }
    22             catch (Exception ex) 
    23             {
    24                 Console.WriteLine(ex.Message);
    25             }
    26         }

    2、任务的取消

     1  static void CancelTask()
     2         {
     3             var cts = new CancellationTokenSource();
     4             cts.Token.Register(() => Console.WriteLine("*** task cancelled"));
     5             cts.CancelAfter(500);
     6 
     7             Task t1 = Task.Run(() => {
     8                 Console.WriteLine("in task ");
     9                 for (int i = 0; i < 20; i++)
    10                 {
    11                     Thread.Sleep(50);
    12                     CancellationToken token = cts.Token;
    13                     if(token.IsCancellationRequested)
    14                     {
    15                         Console.WriteLine("cancelling was requested, cancelling from within the task");
    16                         token.ThrowIfCancellationRequested();
    17                         break;
    18                     }
    19                     Console.WriteLine("in loop");
    20                 }
    21                 Console.WriteLine("task finished without cancellation");
    22             },cts.Token);
    23             try
    24             {
    25                 t1.Wait();
    26             }
    27             catch (AggregateException ex)
    28             {
    29                 Console.WriteLine("exception: {0}, {1}", ex.GetType().Name, ex.Message);
    30                 foreach (var innerException in ex.InnerExceptions)
    31                 {
    32                     Console.WriteLine("inner excepion: {0}, {1}", ex.InnerException.GetType().Name, ex.InnerException.Message);
    33                 }
    34             }
    35         }
  • 相关阅读:
    nethogs 实时查看进程使用流量情况。
    Bash 使用技巧
    Linux源代码编译安装tree命令
    【字体区别】Serif和Sans Serif
    MySQL数据库恢复的经历。
    Linux常用命令
    关于一机多区的可行性分析。
    记一次node节点异常排查
    kube-prometheus部署
    认证、授权与准入控制
  • 原文地址:https://www.cnblogs.com/farmer-y/p/6031005.html
Copyright © 2020-2023  润新知