C# 中提供多线程同步退出机制,详参对象: CancellationTokenSource

 

CancellationTokenSource 中暂未提供复位操作,因此当调用Cancle 之后,若再次调用,需重新初使化对象。

代码示例:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CancellationTokenSourceTest
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //一种多线程取消任务开关对象
            CancellationTokenSource s1 = new CancellationTokenSource();
            CancellationTokenSource s2 = new CancellationTokenSource();

            //s1 , 或者 s2 取消,导致 s3 取消
            CancellationTokenSource s3 = CancellationTokenSource.CreateLinkedTokenSource(s1.Token, s2.Token);

            //异步执行结束 回调
            s1.Token.Register(new Action(() => {
                Console.WriteLine("线程{0} 执行回调!", System.Threading.Thread.CurrentThread.ManagedThreadId);
            }));

            s2.Token.Register(new Action(() =>
            {
                Console.WriteLine("线程{0} 执行回调!", System.Threading.Thread.CurrentThread.ManagedThreadId);
            }));

            s3.Token.Register(new Action(() =>
            {
                Console.WriteLine("线程{0} 执行回调!", System.Threading.Thread.CurrentThread.ManagedThreadId);
            }));

            //异步执行
            ThreadPool.QueueUserWorkItem(new WaitCallback(Print), s1.Token);  //Token 中包含回调信息,执行结束 触发 Register 关联方法。
           // System.Threading.Thread.Sleep(2000);
            ThreadPool.QueueUserWorkItem(new WaitCallback(Print), s2.Token);
          //  System.Threading.Thread.Sleep(2000);
            ThreadPool.QueueUserWorkItem(new WaitCallback(Print), s3.Token);


           
            Console.WriteLine();
            s2.CancelAfter(3000); //若各线程传递各自tocken ,执行回调线程:  s2 , s3
            //s1.CancelAfter(3000); //若各线程传递各自tocken ,执行回调线程:  s1 , s3

            //s3.Cancel(); //若各线程传递各自tocken ,只触发 s3 , 不会触发 s1 , s2 回调。

            //注意: 若各线程传递s3.token ,  s1 , s2 任意Cancle , s1 , s2 , s3 均会回调。
            
            Console.ReadKey();
           

        }


        static void Print(object objToken) {
            CancellationToken token = (CancellationToken)objToken;
            Console.WriteLine("Print , 开始等待{0}...", System.Threading.Thread.CurrentThread.ManagedThreadId);
            if (token.WaitHandle.WaitOne()) { 
                Console.WriteLine( "直到 调用 Cancel() 执行此处 ");
            }
            

            
            //while (!token.IsCancellationRequested) {
            //    //调用Cancel() 后 退出循环
            //}

            Console.WriteLine("执行退出 {0}!" , System.Threading.Thread.CurrentThread.ManagedThreadId);
        }

    }
}

 

 posted on 2016-10-19 17:45  仰光  阅读(6350)  评论(0编辑  收藏  举报