高软玩家

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1)首先实现一个多线程的辅助类,代码如下:

 1 public class ThreadMulti
 2   {
 3       public delegate void DelegateComplete();
 4       public delegate void DelegateWork(int taskindex,int threadindex);
 5  
 6       public DelegateComplete CompleteEvent;
 7       public DelegateWork WorkMethod;
 8  
 9  
10       private ManualResetEvent[] _resets;
11       private int _taskCount = 0;
12       private int _threadCount = 5;
13  
14       public ThreadMulti(int taskcount)
15       {
16           _taskCount = taskcount;
17       }
18  
19       public ThreadMulti(int taskcount, int threadCount)
20       {
21           _taskCount = taskcount;
22           _threadCount = threadCount;
23       }
24  
25       public void Start()
26       {
27           if (_taskCount < _threadCount)
28           {
29               //任务数小于线程数的
30               _resets = new ManualResetEvent[_taskCount];
31               for (int j = 0; j < _taskCount; j++)
32               {
33                   _resets[j] = new ManualResetEvent(false);
34                   ThreadPool.QueueUserWorkItem(new WaitCallback(Work), new object[] { j, j });
35               }
36           }
37           else
38           {
39               _resets = new ManualResetEvent[_threadCount];
40               //任务数大于线程数 先把线程数的任务启动
41               for (int i = 0; i < _threadCount; i++)
42               {
43                   _resets[i] = new ManualResetEvent(false);
44                   ThreadPool.QueueUserWorkItem(new WaitCallback(Work), new object[] { i, i });
45               }
46               //完成一个任务后在利用完成的那个线程执行下一个任务
47               int receivereset = 0;
48               receivereset = ManualResetEvent.WaitAny(_resets);
49               for (int l = _threadCount; l < _taskCount; l++)
50               {
51                   _resets[receivereset].Reset();
52                   ThreadPool.QueueUserWorkItem(new WaitCallback(Work), new object[] { l, receivereset });
53                   receivereset = ManualResetEvent.WaitAny(_resets);
54               }
55           }
56  
57           ManualResetEvent.WaitAll(_resets);
58           if (CompleteEvent != null)
59           {
60               CompleteEvent();
61           }
62       }
63  
64       public void Work(object arg)
65       {
66           int taskindex = int.Parse(((object[])arg)[0].ToString());
67           int resetindex = int.Parse(((object[])arg)[1].ToString());
68           if (WorkMethod != null)
69           {
70               WorkMethod(taskindex + 1, resetindex+1);
71           }
72           _resets[resetindex].Set();
73       }
74   }
View Code
  ThreadMulti类能够根据根据传入的任务数和线程数,实现多线程的重复利用并执行指定的WorkMethod.
  并且在任务完成后能触发CompleteEvent事件.
  类中关键的是ManualResetEvent类的应用,能够在一个任务完成时通知程序哪个线程执行完毕,然后就安排另一个任务开始执行.
 
 
 
2)辅助类的应用
           //实例化多线程辅助类并启动
     ThreadMulti thread = new ThreadMulti(workcount);
 
     thread.WorkMethod = new ThreadMulti.DelegateWork(DoWork);//执行任务的函数
     thread.CompleteEvent = new ThreadMulti.DelegateComplete(WorkComplete);//所有任务执行完毕的事件
     thread.Start();

只需要指定任务数和执行任务的函数和完成任务后的事件即可.调用Start方法后,DoWork会在线程中调用并传递任务的index和执行线程的index.

public void DoWork(int index,int threadindex)
      {
      }

 

   

 

posted on 2017-12-01 17:38  高软玩家  阅读(1593)  评论(0编辑  收藏  举报