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); // 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(20000)) { Console.WriteLine("未收到信号,超时退出!"); } //如下等效 //while (!token.IsCancellationRequested) { // System.Threading.Thread.Sleep(1000); //} Console.WriteLine("执行退出 {0}!" , System.Threading.Thread.CurrentThread.ManagedThreadId); } } }
时间: 2024-10-25 05:31:50