winform开发使用通用多线程基类分享(以队列形式)
本文导语: 代码如下:/// /// 队列多线程,T 代表处理的单个类型~ /// /// public abstract class QueueThreadBase { #region 变量&属性 /// /// 待处理结果 /// private class PendingRe...
///
/// 队列多线程,T 代表处理的单个类型~
///
///
public abstract class QueueThreadBase
{
#region 变量&属性
///
/// 待处理结果
///
private class PendingResult
{
///
/// 待处理值
///
public T PendingValue { get; set; }
///
/// 是否有值
///
public bool IsHad { get; set; }
}
///
/// 线程数
///
public int ThreadCount
{
get { return this.m_ThreadCount; }
set { this.m_ThreadCount = value; }
}
private int m_ThreadCount = 5;
///
/// 取消=True
///
public bool Cancel { get; set; }
///
/// 线程列表
///
List m_ThreadList;
///
/// 完成队列个数
///
private volatile int m_CompletedCount = 0;
///
/// 队列总数
///
private int m_QueueCount = 0;
///
/// 全部完成锁
///
private object m_AllCompletedLock = new object();
///
/// 完成的线程数
///
private int m_CompetedCount = 0;
///
/// 队列锁
///
private object m_PendingQueueLock = new object();
private Queue m_InnerQueue;
#endregion
#region 事件相关
///
/// 全部完成事件
///
public event Action AllCompleted;
///
/// 单个完成事件
///
public event Action OneCompleted;
///
/// 引发全部完成事件
///
///
private void OnAllCompleted(CompetedEventArgs args)
{
if (AllCompleted != null)
{
try
{
AllCompleted(args);//全部完成事件
}
catch { }
}
}
///
/// 引发单个完成事件
///
///
///
private void OnOneCompleted(T pendingValue, CompetedEventArgs args)
{
if (OneCompleted != null)
{
try
{
OneCompleted(pendingValue, args);
}
catch { }
}
}
#endregion
#region 构造
public QueueThreadBase(IEnumerable collection)
{
m_InnerQueue = new Queue(collection);
this.m_QueueCount = m_InnerQueue.Count;
}
#endregion
#region 主体
///
/// 初始化线程
///
private void InitThread()
{
m_ThreadList = new List();
for (int i = 0; i < ThreadCount; i++)
{
Thread t = new Thread(new ThreadStart(InnerDoWork));
m_ThreadList.Add(t);
t.IsBackground = true;
t.Start();
}
}
///
/// 开始
///
public void Start()
{
InitThread();
}
///
/// 线程工作
///
private void InnerDoWork()
{
try
{
Exception doWorkEx = null;
DoWorkResult doworkResult = DoWorkResult.ContinueThread;
var t = CurrentPendingQueue;
while (!this.Cancel && t.IsHad)
{
try
{
doworkResult = DoWork(t.PendingValue);
}
catch (Exception ex)
{
doWorkEx = ex;
}
m_CompletedCount++;
int precent = m_CompletedCount * 100 / m_QueueCount;
OnOneCompleted(t.PendingValue, new CompetedEventArgs() { CompetedPrecent = precent, InnerException = doWorkEx });
if (doworkResult == DoWorkResult.AbortAllThread)
{
this.Cancel = true;
break;
}
else if (doworkResult == DoWorkResult.AbortCurrentThread)
{
break;
}
t = CurrentPendingQueue;
}
lock (m_AllCompletedLock)
{
m_CompetedCount++;
if (m_CompetedCount == m_ThreadList.Count)
{
OnAllCompleted(new CompetedEventArgs() { CompetedPrecent = 100 });
}
}
}
catch
{
throw;
}
}
///
/// 子类重写
///
///
///
protected virtual DoWorkResult DoWork(T pendingValue)
{
return DoWorkResult.ContinueThread;
}
///
/// 获取当前结果
///
private PendingResult CurrentPendingQueue
{
get
{
lock (m_PendingQueueLock)
{
PendingResult t = new PendingResult();
if (m_InnerQueue.Count != 0)
{
t.PendingValue = m_InnerQueue.Dequeue();
t.IsHad = true;
}
else
{
t.PendingValue = default(T);
t.IsHad = false;
}
return t;
}
}
}
#endregion
#region 相关类&枚举
///
/// dowork结果枚举
///
public enum DoWorkResult
{
///
/// 继续运行,默认
///
ContinueThread = 0,
///
/// 终止当前线程
///
AbortCurrentThread = 1,
///
/// 终止全部线程
///
AbortAllThread = 2
}
///
/// 完成事件数据
///
public class CompetedEventArgs : EventArgs
{
public CompetedEventArgs()
{
}
///
/// 完成百分率
///
public int CompetedPrecent { get; set; }
///
/// 异常信息
///
public Exception InnerException { get; set; }
}
#endregion
}
1.从构造函数来看,处理的是一个确定的列表.没错.这个多线程只能处理已经确定的列表,你是否会问.可不可以一边添加,一边处理呢?(呵呵,可以,请联系楼主,当然你也可以自己写,是吧?!)
public QueueThreadBase(IEnumerable collection)
2.提供撤销的功能
///
/// 取消=True
///
public bool Cancel { get; set; }
3.提供线程个数修改功能
///
/// 线程数
///
public int ThreadCount
{
get { return this.m_ThreadCount; }
set { this.m_ThreadCount = value; }
}
4.提供多种事件响应,如单个完成,全部完成的事件
///
/// 全部完成事件
///
public event Action AllCompleted;
///
/// 单个完成事件
///
public event Action OneCompleted;
5.提供完成的百分率
///
/// 完成事件数据
///
public class CompetedEventArgs : EventArgs
{
public CompetedEventArgs()
{
}
///
/// 完成百分率
///
public int CompetedPrecent { get; set; }
///
/// 异常信息
///
public Exception InnerException { get; set; }
}
6.提供终止线程的方式,继续/单线程终止/全部终止
///
/// dowork结果枚举
///
public enum DoWorkResult
{
///
/// 继续运行,默认
///
ContinueThread = 0,
///
/// 终止当前线程
///
AbortCurrentThread = 1,
///
/// 终止全部线程
///
AbortAllThread = 2
}
你是否会问?怎么用呢?别急....请看
///
/// 下载线程对了.
///
public class DownLoadQueueThread:QueueThreadBase
{
///
///
///
/// 下载的列表ID
public DownLoadQueueThread(IEnumerable list):base(list)
{
}
///
/// 每次多线程都到这里来,处理多线程
///
///
///
protected override DoWorkResult DoWork(int pendingID)
{
try
{
//..........多线程处理....
return DoWorkResult.ContinueThread;//没有异常让线程继续跑..
}
catch (Exception)
{
return DoWorkResult.AbortCurrentThread;//有异常,可以终止当前线程.当然.也可以继续,
//return DoWorkResult.AbortAllThread; //特殊情况下 ,有异常终止所有的线程...
}
//return base.DoWork(pendingValue);
}
}
用法
总结:
多线程在什么时候都会用到.不用到是你不会用.多线程要一定的编程基础,如果你觉得有点难度,那你可以学习并且借鉴人家已有的东西.少走弯路,是我们程序员经历嗷嗷待哺后的心声.本文以交流态度和感恩心态,贡献给有需要的人们.