C#如何对多线程、多任务管理(demo)
作者:clarlespeng
这篇文章主要通过一个小demo介绍了C#如何对多线程、多任务管理,需要的朋友可以参考下
下面一段内容有项目需求有项目分析,通过一个小demo给大家展示下C#如何对多线程、多任务管理的。
项目需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中间执行状况);
多个任务 根据条件不同 可能需要不同的处理
项目分析: 多线程并发执行多任务;
对任务进行管理,追踪中间执行状态;
运用策略模式抽象执行类;
public enum TaskStatus { wait = 0, working = 1, stop = 2, suspend = 3, complete = 4, fail = 5, other = 99 } public class Task { public TaskDelegate<ParaStatus> taskStatus; public TaskDelegate<ParaInfo> taskInfo; public TaskDelegate<ParaProcess> taskProcess; private TaskStatus _status = TaskStatus.wait; private IExecuter _Executer { get; set; } private Guid _taskId; private Thread thread; private string _name; public Task(Guid id, IExecuter Executer) { _taskId = id; _Executer = Executer; _Executer.TaskId = id; _Executer.Task = this; } public void Wait() { ChangeStatus(TaskStatus.wait); } public string Name { get { return _name; } set { _name = value; } } public Guid TaskId { get { return _taskId; } } public TaskStatus Status { get { return _status; } set { _status = value; } } public void Start() { if (thread == null) { thread = new Thread(_Executer.Do); } else if (thread.ThreadState == ThreadState.Stopped) { thread = new Thread(_Executer.Do); } thread.Start(); ChangeStatus(TaskStatus.working); } public void ChangeStatus(TaskStatus s) { Status = s; ParaStatus ps = new ParaStatus(TaskId); ps.Status = Status; if (taskStatus != null) taskStatus(ps); } public void Stop() { if (thread != null) { if (thread.IsAlive) { try { thread.Abort(); } catch { } } } ChangeStatus(TaskStatus.stop); } public void Suspend() { try { thread.Suspend(); } catch { } ChangeStatus(TaskStatus.suspend); } public void Resume() { if (thread.IsAlive) { thread.Resume(); } ChangeStatus(TaskStatus.working); } } public interface IExecuter { Guid TaskId { get; set; } Task Task { get; set; } void Do(); } public class Executer : IExecuter { public void Execute() { } #region IExecute public Guid TaskId { get; set; } public Task Task { get; set; } public void Do() { for (int i = 1; i <= 100; i++) { if (Task.taskProcess != null) { Thread.Sleep(1000); ParaProcess pp = new ParaProcess(TaskId); pp.Rate = (double)i / 100; Task.taskProcess(pp); } } if (Task.taskStatus != null) { Task.ChangeStatus(TaskStatus.complete); } } #endregion } public class TaskManager { public List<Task> Tasks = new List<Task>(); /// <summary> /// 添加任务 /// </summary> public Task AddTask(Task task) { try { Tasks.Add(task); task.Wait(); } catch (Exception e) { } return task; } public void StartTask(Task task) { task.Start(); } /// <summary> /// 停止任务 /// </summary> /// <param name="downloader"></param> public void StopTask(Task task) { task.Stop(); } public void SuspendTask(Task task) { task.Suspend(); } public void ResumeTask(Task task) { task.Resume(); } /// <summary> /// 删除任务(自动终止未停止的任务) /// </summary> /// <param name="downloader"></param> public void DeleteTask(Task task, bool deleteFile) { //先停止任务 task.Stop(); while (task.Status == TaskStatus.working) { Thread.Sleep(50); } //从任务列表中删除任务 if (Tasks.Contains(task)) { Tasks.Remove(task); } } } /// </summary> /// <param name="para">泛型参数</param> public delegate void TaskDelegate<T>(T para); /// <summary> /// /// </summary> public class ParaStatus { public ParaStatus(Guid task) { TaskId = task; } public Guid TaskId { get; set; } public TaskStatus Status { get; set; } } public class ParaInfo { public ParaInfo(Guid task) { TaskId = task; } public Guid TaskId { get; set; } public string Info { get; set; } } public class ParaProcess { public ParaProcess(Guid task) { TaskId = task; } public Guid TaskId { get; set; } public double Rate { get; set; } }
以上代码内容就是C#对多线程、多任务管理的方法,希望对大家有所帮助。