// 自己写的一个任务机,相比一般的任务机的有点,此任务机可以定时执行任务。注释已很明确,不多解释了
/// <summary> /// 任务机 /// </summary> public class TaskRunner { /// <summary> /// 任务执行完之后的回调方法类型 /// </summary> /// <param name="arg0">使用LoadTask添加任务时传入的arg0</param> /// <param name="arg1">使用LoadTask添加任务时传入的arg1</param> /// <param name="arg2">使用LoadTask添加任务时传入的arg2</param> /// <returns></returns> public delegate int Handler(int arg0, object arg1, byte[] arg2); /// <summary> /// 任务描述 /// </summary> struct TaskInfo { /// <summary> /// 任务唯一id /// </summary> static int _id_ = 1; public TaskInfo(int iType, Handler handler, int arg0, object arg1, byte[] arg2, long tmOffset = 0) { this.id = _id_++; this.iType = iType; this.handler = handler; this.arg0 = arg0; this.arg1 = arg1; this.arg2 = arg2; this.trigTick = DateTime.Now.AddMilliseconds(tmOffset).Ticks; } public int id; public int iType; public Handler handler; public int arg0; public object arg1; public byte[] arg2; public long trigTick; // 任务执行时机,只有在此值大于或等于当前时间时,任务才会被触发 } private List<TaskInfo> m_lsTask = null; // 储存任务的list private bool m_bStop = true; private Thread m_ThreadRunner = null; // 用于执行任务的线程 public TaskRunner() { m_lsTask = new List<TaskInfo>(); } /// <summary> /// 添加任务 /// </summary> /// <param name="iType">任务类型,由用户自定义,若不对任务分类则传0</param> /// <param name="handler">任务回调</param> /// <param name="arg0">用户定义参数</param> /// <param name="arg1">用户定义参数</param> /// <param name="arg2">用户定义参数</param> /// <param name="tmOffset">任务执行时机,一个相对于当前时间的偏移值,单位毫秒</param> /// <returns>返回任务id</returns> public int LoadTask(int iType, Handler handler, int arg0, object arg1, byte[] arg2, long tmOffset = 0) { TaskInfo ti = new TaskInfo(iType, handler, arg0, arg1, arg2, tmOffset); lock (m_lsTask) { m_lsTask.Add(ti); } return ti.id; } /// <summary> /// 卸载任务,若id<1则根据iType卸载,若id>0则根据id卸载并忽略iType /// </summary> /// <param name="id">任务id,从LoadTask的返回值获取</param> /// <param name="iType">任务类型,由用户自定义,必须大于0</param> /// <returns>返回成功卸载的任务个数</returns> public int UnloadTask(int id, int iType) { int iCount = 0; lock (m_lsTask) { for (int i = 0; i < m_lsTask.Count;i++ ) { if (id < 1) { if (m_lsTask[i].iType == iType) { m_lsTask.RemoveAt(i); iCount++; i = -1; } } else if (m_lsTask[i].id == id) { m_lsTask.RemoveAt(i); iCount++; break; } } } return iCount; } /// <summary> /// 执行任务的线程 /// </summary> void ThreadRunner() { m_lsTask.Clear(); while (!m_bStop) { Thread.Sleep(50); TaskInfo? taskInfo = null; lock (m_lsTask) { foreach (TaskInfo item in m_lsTask) { if (item.trigTick < DateTime.Now.Ticks) { taskInfo = item; m_lsTask.Remove(item); break; } } } if (taskInfo != null) { taskInfo.Value.handler(taskInfo.Value.arg0, taskInfo.Value.arg1, taskInfo.Value.arg2); } } m_lsTask.Clear(); } /// <summary> /// 开始任务机 /// </summary> internal void Start() { m_bStop = false; m_ThreadRunner = new Thread(new ThreadStart(ThreadRunner)); m_ThreadRunner.Start(); } /// <summary> /// 停止任务机 /// </summary> internal void Stop() { m_bStop = true; m_ThreadRunner.Join(3000); } }
用C#实现的一个简单任务机,解决了一般任务机无法定时执行任务的问题
最新推荐文章于 2022-07-04 17:24:39 发布