zoukankan      html  css  js  c++  java
  • 27 线程执行上下文和取消

    27.3 执行上下文

                CallContext.LogicalSetData("Name", "Jeffrey");
                ThreadPool.QueueUserWorkItem(obj =>
                {
                    Console.WriteLine($"Name={CallContext.LogicalGetData("Name")}");
                });
                ExecutionContext.SuppressFlow();
                ThreadPool.QueueUserWorkItem(obj =>
                {
                    Console.WriteLine($"Name={CallContext.LogicalGetData("Name")}");
                });
                ExecutionContext.RestoreFlow();

     27.4 协作式取消和超时

            static void Main(string[] args)
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                ThreadPool.QueueUserWorkItem(o => Count(cts.Token, 1000));
                Console.WriteLine("press enter to cancel the operation");
                Console.ReadLine();
                cts.Cancel();
                Console.ReadKey();
            }
            private static void Count(CancellationToken token, int countTo)
            {
                for (int count = 0; count < countTo; count++)
                {
                    if (token.IsCancellationRequested)
                    {
                        Console.WriteLine("Count is cancel");
                        break;
                    }
                    Console.WriteLine(count);
                    Thread.Sleep(200);
                }
                Console.WriteLine("Count is done");
            }
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.Token.Register(() => { Console.WriteLine("Canceled 1"); });
                cts.Token.Register(() => { Console.WriteLine("Canceled 2"); });
                cts.Cancel();
    
                CancellationTokenSource cts1 = new CancellationTokenSource();
                cts1.Token.Register(() => { Console.WriteLine("Cts1 canceled"); });
    
                CancellationTokenSource cts2 = new CancellationTokenSource();
                cts2.Token.Register(() => { Console.WriteLine("Cts2 canceled"); });
    
                var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cts1.Token, cts2.Token);
                linkedCts.Token.Register(() => { Console.WriteLine("linked cancel"); });
    
                cts2.Cancel();
    
                Console.WriteLine("cts1={0} cts2={1} linked={2}", cts1.IsCancellationRequested, cts2.IsCancellationRequested, linkedCts.IsCancellationRequested);
                /*  Canceled 2
                Canceled 1
                linked cancel
                Cts2 canceled
                cts1=False cts2=True linked=True    */

     

  • 相关阅读:
    前端的推荐资源
    Python 学习日志(一)
    遇到的一些elasticsearch报错信息整理
    记hyper-v导致的privoxy error(fatal error: can't bind to 127.0.0.1:1081(error number:0)),附解决方法
    Java动态代理学习笔记
    spring依赖注入中p命名空间和c命名空间区别
    python "二维" 字典(字典嵌套)
    [剑指offer] 数组中的逆序对
    [剑指offer] 复杂链表的复制
    [剑指offer] 8-10做题笔记
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/10198740.html
Copyright © 2011-2022 走看看