首页 > 代码库 > 线程基类 转载滴,收藏一下

线程基类 转载滴,收藏一下

 
转载请注明出处、http://www.cnblogs.com/zetee/p/3486993.html

多线程这个概念大家都很熟悉,对于winform的开发人员来说.用的还是多的.但估计都是用Timer,或者backgroundWorker.

你是否曾经想过,写一个基类,然后....一用到多线程的时候,就马上能用上呢.

没错,福利来了,这面我为大家写了多线程的一个基类.只有你用到多线程,下面的代码肯定能帮到你很多忙
/// <summary>    /// 下载线程对了.    /// </summary>    public class DownLoadQueueThread:QueueThreadBase<int>    {        /// <summary>        ///         /// </summary>        /// <param name="list">下载的列表ID</param>        public DownLoadQueueThread(IEnumerable<int> list):base(list)        {        }        /// <summary>        /// 每次多线程都到这里来,处理多线程        /// </summary>        /// <param name="pendingValue"列表ID></param>        /// <returns></returns>        protected override DoWorkResult DoWork(int pendingID)        {            try            {                //..........多线程处理....                return DoWorkResult.ContinueThread;//没有异常让线程继续跑..            }            catch (Exception)            {                return DoWorkResult.AbortCurrentThread;//有异常,可以终止当前线程.当然.也可以继续,                //return  DoWorkResult.AbortAllThread; //特殊情况下 ,有异常终止所有的线程...            }            //return base.DoWork(pendingValue);        }    }
View Code

 

  1 /// <summary>  2     /// 队列多线程,T 代表处理的单个类型~  3     /// </summary>  4     /// <typeparam name="T"></typeparam>  5     public abstract class QueueThreadBase<T>  6     {  7         #region 变量&属性  8         /// <summary>  9         /// 待处理结果 10         /// </summary> 11         private class PendingResult 12         { 13             /// <summary> 14             /// 待处理值 15             /// </summary> 16             public T PendingValue { get; set; } 17             /// <summary> 18             /// 是否有值 19             /// </summary> 20             public bool IsHad { get; set; } 21         } 22         /// <summary> 23         /// 线程数 24         /// </summary> 25         public int ThreadCount 26         { 27             get { return this.m_ThreadCount; } 28             set { this.m_ThreadCount = value; } 29         } 30         private int m_ThreadCount = 5; 31         /// <summary> 32         /// 取消=True 33         /// </summary> 34         public bool Cancel { get; set; } 35         /// <summary> 36         /// 线程列表 37         /// </summary> 38         List<Thread> m_ThreadList; 39         /// <summary> 40         /// 完成队列个数 41         /// </summary> 42         private volatile int m_CompletedCount = 0; 43         /// <summary> 44         /// 队列总数 45         /// </summary> 46         private int m_QueueCount = 0; 47         /// <summary> 48         /// 全部完成锁 49         /// </summary> 50         private object m_AllCompletedLock = new object(); 51         /// <summary> 52         /// 完成的线程数 53         /// </summary> 54         private int m_CompetedCount = 0; 55         /// <summary> 56         /// 队列锁 57         /// </summary> 58         private object m_PendingQueueLock = new object(); 59         private Queue<T> m_InnerQueue; 60         #endregion 61  62  63         #region 事件相关 64         /// <summary> 65         /// 全部完成事件 66         /// </summary> 67         public event Action<CompetedEventArgs> AllCompleted; 68         /// <summary> 69         /// 单个完成事件 70         /// </summary> 71         public event Action<T, CompetedEventArgs> OneCompleted; 72         /// <summary> 73         /// 引发全部完成事件 74         /// </summary> 75         /// <param name="args"></param> 76         private void OnAllCompleted(CompetedEventArgs args) 77         { 78             if (AllCompleted != null) 79             { 80                 try 81                 { 82                     AllCompleted(args);//全部完成事件 83                 } 84                 catch { } 85             } 86         } 87         /// <summary> 88         /// 引发单个完成事件 89         /// </summary> 90         /// <param name="pendingValue"></param> 91         /// <param name="args"></param> 92         private void OnOneCompleted(T pendingValue, CompetedEventArgs args) 93         { 94             if (OneCompleted != null) 95             { 96                 try 97                 { 98                     OneCompleted(pendingValue, args); 99                 }100                 catch { }101 102             }103         } 104         #endregion105 106         #region 构造107         public QueueThreadBase(IEnumerable<T> collection)108         {109             m_InnerQueue = new Queue<T>(collection);110             this.m_QueueCount = m_InnerQueue.Count;111         }112         113         #endregion114 115         #region 主体116         /// <summary>117         /// 初始化线程118         /// </summary>119         private void InitThread()120         {121             m_ThreadList = new List<Thread>();122             for (int i = 0; i < ThreadCount; i++)123             {124                 Thread t = new Thread(new ThreadStart(InnerDoWork));125         m_ThreadList.Add(t);126                 t.IsBackground = true;127                 t.Start();128             }129         }130         /// <summary>131         /// 开始132         /// </summary>133         public void Start()134         {135             InitThread();136         }137         /// <summary>138         /// 线程工作139         /// </summary>140         private void InnerDoWork()141         {142             try143             {144                 Exception doWorkEx = null;145                 DoWorkResult doworkResult = DoWorkResult.ContinueThread;146                 var t = CurrentPendingQueue;147                 while (!this.Cancel && t.IsHad)148                 {149                     try150                     {151                         doworkResult = DoWork(t.PendingValue);152                     }153                     catch (Exception ex)154                     {155                         doWorkEx = ex;156                     }157                     m_CompletedCount++;158                     int precent = m_CompletedCount * 100 / m_QueueCount;159                     OnOneCompleted(t.PendingValue, new CompetedEventArgs() { CompetedPrecent = precent, InnerException = doWorkEx });160                     if (doworkResult == DoWorkResult.AbortAllThread)161                     {162                         this.Cancel = true;163                         break;164                     }165                     else if (doworkResult == DoWorkResult.AbortCurrentThread)166                     {167                         break;168                     }169                     t = CurrentPendingQueue;170                 }171 172                 lock (m_AllCompletedLock)173                 {174                     m_CompetedCount++;175                     if (m_CompetedCount == m_ThreadList.Count)176                     {177                         OnAllCompleted(new CompetedEventArgs() { CompetedPrecent = 100 });178                     }179                 }180 181             }182             catch 183             {184                 throw;185             }186         }187         /// <summary>188         /// 子类重写189         /// </summary>190         /// <param name="pendingValue"></param>191         /// <returns></returns>192         protected virtual DoWorkResult DoWork(T pendingValue)193         {194             return DoWorkResult.ContinueThread;195         }196         /// <summary>197         /// 获取当前结果198         /// </summary>199         private PendingResult CurrentPendingQueue200         {201             get202             {203                 lock (m_PendingQueueLock)204                 {205                     PendingResult t = new PendingResult();206                     if (m_InnerQueue.Count != 0)207                     {208                         t.PendingValue =http://www.mamicode.com/ m_InnerQueue.Dequeue();209                         t.IsHad = true;210                     }211                     else212                     {213                         t.PendingValue = http://www.mamicode.com/default(T);214                         t.IsHad = false;215                     }216                     return t;217                 }218             }219         }220          221         #endregion222 223         #region 相关类&枚举224         /// <summary>225         /// dowork结果枚举226         /// </summary>227         public enum DoWorkResult228         {229             /// <summary>230             /// 继续运行,默认231             /// </summary>232             ContinueThread = 0,233             /// <summary>234             /// 终止当前线程235             /// </summary>236             AbortCurrentThread = 1,237             /// <summary>238             /// 终止全部线程239             /// </summary>240             AbortAllThread = 2241         }242         /// <summary>243         /// 完成事件数据244         /// </summary>245         public class CompetedEventArgs : EventArgs246         {247             public CompetedEventArgs()248             {249 250             }251             /// <summary>252             /// 完成百分率253             /// </summary>254             public int CompetedPrecent { get; set; }255             /// <summary>256             /// 异常信息257             /// </summary>258             public Exception InnerException { get; set; }259         } 260         #endregion261 262     }
View Code