当前位置: 代码迷 >> C# >> 多线程、多任务管理 简略demo
  详细解决方案

多线程、多任务管理 简略demo

热度:39   发布时间:2016-05-05 03:08:06.0
多线程、多任务管理 简单demo

需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中间执行状况);

         多个任务 根据条件不同 可能需要不同的处理

分析: 多线程并发执行多任务;

         对任务进行管理,追踪中间执行状态;

         运用策略模式抽象执行类; 

实现:

   

 1 public enum TaskStatus 2     { 3         wait = 0, 4         working = 1, 5         stop = 2, 6         suspend = 3, 7         complete = 4, 8         fail = 5, 9         other = 9910     }
TaskStatus
  1 public class Task  2     {  3         public TaskDelegate<ParaStatus> taskStatus;  4         public TaskDelegate<ParaInfo> taskInfo;  5         public TaskDelegate<ParaProcess> taskProcess;  6   7         private TaskStatus _status = TaskStatus.wait;  8         private IExecuter _Executer { get; set; }  9         private Guid _taskId; 10         private Thread thread; 11         private string _name; 12  13         public Task(Guid id, IExecuter Executer) 14         { 15             _taskId = id; 16             _Executer = Executer; 17             _Executer.TaskId = id; 18             _Executer.Task = this; 19         } 20  21         public void Wait() 22         { 23             ChangeStatus(TaskStatus.wait); 24         } 25  26         public string Name 27         { 28             get 29             { 30                 return _name; 31             } 32             set 33             { 34                 _name = value; 35             } 36         } 37          38         public Guid TaskId 39         { 40             get 41             { 42                 return _taskId; 43             } 44         } 45          46         public TaskStatus Status 47         { 48             get 49             { 50                 return _status; 51             } 52             set 53             { 54                 _status = value; 55             } 56         }         57  58         public void Start() 59         { 60             if (thread == null) 61             { 62                 thread = new Thread(_Executer.Do); 63             } 64             else if (thread.ThreadState == ThreadState.Stopped) 65             { 66                 thread = new Thread(_Executer.Do); 67             } 68             thread.Start(); 69             ChangeStatus(TaskStatus.working); 70         } 71         public void ChangeStatus(TaskStatus s) 72         { 73             Status = s; 74             ParaStatus ps = new ParaStatus(TaskId); 75             ps.Status = Status; 76             if (taskStatus != null) taskStatus(ps); 77         } 78         public void Stop() 79         { 80             if (thread != null) 81             { 82                 if (thread.IsAlive) 83                 { 84                     try 85                     { 86                         thread.Abort(); 87                     } 88                     catch { } 89                 } 90             } 91             ChangeStatus(TaskStatus.stop); 92         } 93         public void Suspend() 94         { 95             try 96             { 97                 thread.Suspend(); 98             } 99             catch { }100             ChangeStatus(TaskStatus.suspend);101         }102         public void Resume()103         {104             if (thread.IsAlive)105             {106                 thread.Resume();107             }108             ChangeStatus(TaskStatus.working);109         }110     }
Task
1  public interface IExecuter2     {3         Guid TaskId { get; set; }4 5         Task Task { get; set; }6         void Do();7     }
IExecuter
 1 public class Executer : IExecuter 2     { 3         public void Execute() 4         { 5  6         } 7         #region IExecute 8         public Guid TaskId { get; set; } 9 10         public Task Task { get; set; }11 12         public void Do()13         {14             for (int i = 1; i <= 100; i++)15             {16                 if (Task.taskProcess != null)17                 {18                     Thread.Sleep(1000);19                     ParaProcess pp = new ParaProcess(TaskId);20                     pp.Rate = (double)i / 100;21                     Task.taskProcess(pp); 22                 }23             }24             if (Task.taskStatus != null)25             {26                 Task.ChangeStatus(TaskStatus.complete);27             }28         }29         #endregion30     }
Executer
 1 public class TaskManager 2     { 3         public List<Task> Tasks = new List<Task>(); 4         /// <summary> 5         /// 添加任务 6         /// </summary> 7         public Task AddTask(Task task) 8         { 9             try10             {11                 Tasks.Add(task);12 13                 task.Wait();14             }15             catch (Exception e)16             {17 18             }19             return task;20         }21         public void StartTask(Task task)22         {23             task.Start();24         }25         /// <summary>26         /// 停止任务27         /// </summary>28         /// <param name="downloader"></param>29         public void StopTask(Task task)30         {31             task.Stop();32         }33 34         public void SuspendTask(Task task)35         {36             task.Suspend();37         }38 39         public void ResumeTask(Task task)40         {41             task.Resume();42         }43 44         /// <summary>45         /// 删除任务(自动终止未停止的任务)46         /// </summary>47         /// <param name="downloader"></param>48         public void DeleteTask(Task task, bool deleteFile)49         {50             //先停止任务51             task.Stop();52 53             while (task.Status == TaskStatus.working)54             {55                 Thread.Sleep(50);56             }57 58             //从任务列表中删除任务59             if (Tasks.Contains(task))60             {61                 Tasks.Remove(task);62             }63         }64 65     }
TaskManager
 1  /// </summary> 2     /// <param name="para">泛型参数</param> 3     public delegate void TaskDelegate<T>(T para); 4  5     /// <summary> 6     ///  7     /// </summary> 8     public class ParaStatus 9     {10         public ParaStatus(Guid task) { TaskId = task; }11         public Guid TaskId { get; set; }12         public TaskStatus Status { get; set; }13     }    14 15     public class ParaInfo16     {17         public ParaInfo(Guid task) { TaskId = task; }18         public Guid TaskId { get; set; }19         public string Info { get; set; }20     }21 22     public class ParaProcess23     {24         public ParaProcess(Guid task) { TaskId = task; }25         public Guid TaskId { get; set; }26         public double Rate { get; set; }        27     }
TaskDelegate

demo 下载:demo

  相关解决方案