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    */

     

  • 相关阅读:
    Asp.net mvc 使用Ajax调用Action 返回数据。
    浅析Asp.net MVC 中Ajax的使用
    Asp.net MVC2中使用Ajax的三种方式
    Ajaxa请求:参数、传递的数据、返回的数据
    ASP.NET MVC 模块与组件——发送邮件
    MVC中根据后台绝对路径读取图片并显示在IMG中
    MVC、一般处理程序hanlder 输出图片文件
    邮箱验证功能原理 语法 属性
    C# 注册邮箱验证的实现代码
    注册页实现激活邮箱验证(asp.net c#) 详细实现
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/10198740.html
Copyright © 2011-2022 走看看