VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#如何对多线程、多任务管理(demo)

  • 2018-10-29 10:11 来源:未知

下面一段内容有项目需求有项目分析,通过一个小demo给大家展示下C#如何对多线程、多任务管理的。

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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#对多线程、多任务管理的方法,希望对大家有所帮助。

相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号