zoukankan      html  css  js  c++  java
  • C# 实现一个可取消的多线程操作 示例

            private void button1_Click(object sender, EventArgs e)
            {
                //定义一个为可取消资源标志
                CancellationTokenSource cts = new CancellationTokenSource();
                //定义二个为可取消资源标志
                CancellationTokenSource cts1 = new CancellationTokenSource();
    
                //实现一个可取消操作的回调函数,
                ThreadPool.QueueUserWorkItem(o => Count(cts.Token, 1000000));
    
                //为可取消资源标志注册取消后的回调函数(无参,无返回值,匿名委托)
                cts.Token.Register(() => Console.WriteLine("Canceled 1"));            
    
                //为可取消资源标志注册取消后的回调函数(有参,无返回值,显式申明委托)
                cts.Token.Register(o => TestCancellationMethead(1000, 10), true);
    
                cts1.Token.Register(() => Console.WriteLine("Canceled 2"));
    
                ///连接两个可取消资源标志
                var LinkedCts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cts1.Token);
                ///给连接后的可取消资源标志集注册回调函数(匿名委托),集合中任意一个可取消资源标志取消,都将触发该回调函数
                LinkedCts.Token.Register(() => Console.WriteLine("linkedCts canceled"));
                Thread.Sleep(300);
                cts.Cancel();
            }
    
    
            void TestCancellationMethead(int x, int y)
            {
                Console.WriteLine("取消后的回调函数" + x + y);
            }
    
            /// <summary>
            /// 一个可取消操作的回调函数,函数的关键是传入一个CancellationToken对象
            /// </summary>
            /// <param name="token"></param>
            /// <param name="countTo"></param>
            void Count(CancellationToken token, Int32 countTo)
            {
                Console.WriteLine("一个可取消的操作开始执行");
                for (int count = 0; count < countTo; count++)
                {
                    if (token.IsCancellationRequested)
                    {
                        Console.WriteLine("$" + count.ToString());
                        Console.ReadLine();
                        break;
                    }
                    Console.WriteLine("|" + count.ToString());
                    Console.ReadLine();
                }
            }
            bool IsCancellationRequested = false;
    
            private void button2_Click(object sender, EventArgs e)
            {
                ThreadPool.QueueUserWorkItem(o => Count(1000000));
                Thread.Sleep(300);
                IsCancellationRequested = true;
            }
    
            void Count(Int32 countTo)
            {
                Console.WriteLine("一个可取消的操作开始执行");
                for (int count = 0; count < countTo; count++)
                {
                    if (IsCancellationRequested)
                    {
                        Console.WriteLine("一个可取消的操作被取消" + count.ToString());
                        break;
                    }
                    Console.WriteLine(count.ToString());
                }
            }
  • 相关阅读:
    spark streaming 入门例子
    ElasticSearch-hadoop saveToEs源码分析
    spark 资源参数调优
    spark 任务运行原理
    spark RDD底层原理
    用实例说明Spark stage划分原理
    Spark任务提交底层原理
    spark shuffle内在原理说明
    iOS 辛格尔顿
    CodeForces 22D Segments 排序水问题
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/5555563.html
Copyright © 2011-2022 走看看