管理异步线程
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace PortClient { /// <summary> /// 异步线程 /// *修改为枚举类型更合理 /// </summary> public static class TaskWorkType { public static string WCFHeartBit = "WCFHeartBit"; public static string Alarm = "AlarmEngine"; public static string Upload = "Upload"; public static string SysStatus = "SysStatus"; public static string OPCServer = "OPCServer"; public static string StationState = "StationState"; public static string PLC = "PLC"; public static string HistoryClear = "HistoryClear"; //public static string WCFHeartBit = "WCFHeartBit"; } /// <summary> /// 工作任务 /// </summary> public class TaskWork { #region 属性 /// <summary>中断 </summary> public bool Stop = false; /// <summary>线程结束回调 </summary> public static event Action<TaskWork> TaskEndEventHandler; public Task Task { get; } public string TaskName { get; } /// <summary>取消线程标记</summary> public CancellationTokenSource CTS { get; private set; } = new CancellationTokenSource(); #endregion /// <summary> /// 创建运行任务 /// </summary> /// <param name="doWork"></param> /// <param name="taskName"></param> public TaskWork(Action<TaskWork> doWork, string taskName) { var token = CTS.Token; TaskName = taskName; var action = new Action(() => { doWork.Invoke(this); }); Task = new Task(action, token, TaskCreationOptions.LongRunning); //添加线程事件 Task.ContinueWith(t => { if (t.IsCompleted) { Console.WriteLine($"{TaskName} Task completed successfully."); } else if (t.IsCanceled) { Console.WriteLine($"{TaskName} Task was canceled."); } else if (t.IsFaulted) { Console.WriteLine($"{TaskName} Task faulted with exception: " + t.Exception.InnerException.Message); } //线程结束事件回调 TaskWork.TaskEndEventHandler?.Invoke(this); TaskEndEventHandler?.Invoke(this); }).ContinueWith(t => { Console.WriteLine("释放内存");//待实现 }); Task.Start(); } #region 静态函数 /// <summary> /// 取消所有线程 /// </summary> public static void CancelThread() { //CTS.Cancel(); //CTS.CancelAfter(10000); TaskDic1.Values.ToList().ForEach(task => { task.Stop = true; task.CTS.CancelAfter(5000); }); } /// <summary> /// 等待所有线程结束 /// /// 使用示例:TaskWork.RunCTS(DBTaskRunningBody, TaskWorkType.Alarm); /// 结束所有线程示例: TaskWork.CancelThread(); /// </summary> public static void WaitAllEnd() { TaskDic1.Values.ToList().ForEach(task => { task.Stop = true;// task.CTS.CancelAfter(1000);//不管有没有完成,强制3秒后退出 }); //等待所有线程结束 //Task.WaitAll(TaskDic1.Values.Select(a => a.Task).ToArray(), 3000); Task.WaitAll(TaskDic1.Values.Select(a => a.Task).ToArray()); } public static Dictionary<string, TaskWork> TaskDic1 = new Dictionary<string, TaskWork>(); /// <summary> /// 异步线程创建;<br/> /// --------code-----<br/> /// taskwork.Stop 判断是否停止运行<br/> /// -----------------<br/> /// 使用TaskHelper.CancelThread() 取消所有任务; /// </summary> /// <param name="DoWork">调用函数参数 CancellationToken</param> /// <param name="TaskName"></param> public static void RunCTS(Action<TaskWork> DoWork, string TaskName) { try { var taskItem1 = new TaskWork(DoWork, TaskName); TaskDic1.Add(TaskName, taskItem1); } catch (Exception ex) { } } #endregion } }
标签:异步,Task,string,C#,static,线程,TaskName,public From: https://www.cnblogs.com/orp1989/p/18222686