/// <summary> /// 往线程池里添加线程 /// </summary> /// <param name="threadName">线程名称</param> /// <returns></returns> public static int AddThread(string threadName) { SimpleThread mThread = new SimpleThread(); mThread.ID = ThreadCount; mThread.ThreadName = threadName; mThread.ThreadType = ""; mThread.BeginTime = DateTime.Now; mThread.RunTimes = "00:00:00"; mThread.RemainTime = "00:00:00"; mThread.IgnoreCount = 0; mThread.SucceedCount = 0; mThread.FailedCount = 0; mThread.PriorityName = "普通"; mThread.StatusName = "运行"; mThread.Status = (int)ThreadStates.Running; mThread.Message = ""; mThread.ObjThread = null; ThreadList.Add(mThread); return(ThreadCount - 1); }
/// <summary> /// 执行线程数据 /// </summary> /// <param name="mThread">当前线程</param> private void ExecuteHandleData(SimpleThread mThread) { DataTable dtData = null; //TimeSpan ts = DateTime.Now - mThread.BeginTime; switch (HandleType) { case -1: //执行代码 HandleData(null); break; case 0: //获取所有要发送的数据 dtData = GetDataTable(false, null); while (dtData != null && dtData.Rows.Count > 0) { //如果停止则跳出 if (mThread.Status == (int)ThreadStates.Stopped) { break; } //执行代码 HandleData(dtData); dtData = GetDataTable(false, null); //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); } break; default: WriteExceptionLog("未知的处理类型!"); mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; mThread.Message = "未知的处理类型"; break; } }
///// <summary> ///// 默认构造函数 ///// <param name="handleType">线程要处理的数据类型(-1:无处理任何类型;0:集合类型)</param> ///// </summary> //protected ThreadExecuteService(int handleType) //{ // //_handleType = handleType; // _mQueryParamater = QueryParamater.Create(); // _mSimpleResult = new SimpleResult(); //} #endregion #region 基本方法 #region IThreadExecuteService成员 /// <summary> /// 执行方法 /// </summary> public virtual void Execute() { //当前线程实例 SimpleThread mThread = this.MThread; if (mThread == null) { WriteExceptionLog("当前线程实例为空!"); return; } //TimeSpan ts = DateTime.Now - mThread.BeginTime; try { int interval = 0; int continuceTeme = 0; while (true) { ConnectionStringsName = mThread.ConnectionStringName; if (mThread.ExecuteType == (int)ExecuteTypes.Continue)//持续执行 { #region 持续执行 //间隔时间 continuceTeme = DataTypeConvert.ToInt32(mThread.Time);//秒 if (continuceTeme <= 0) { continuceTeme = 30; } interval = continuceTeme * 1000;//毫秒 //如果停止则退出 if (mThread.Status == (int)ThreadStates.Stopped) { //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } try { //执行线程数据 ExecuteHandleData(mThread); //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); } catch (Exception ex) { WriteExceptionLog(ex); mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; mThread.Message = "异常:" + ex.Message; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } Thread.Sleep(interval);//等待下次执行 #endregion } else if (mThread.ExecuteType == (int)ExecuteTypes.Timer)//定时执行 { #region 定时执行 if (mThread.TimerType == (int)TimerTypes.Day)//每天 { #region 每天 //定时时间是否异常 DateTime executeTimer = DataTypeConvert.ToDateTime(mThread.Time);//小时:分钟 if (executeTimer == DateTime.MinValue) { mThread.Message = "执行时间(小时:分钟,如:00:30)异常!"; mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } try { //如果停止则退出 if (mThread.Status == (int)ThreadStates.Stopped) { //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } //小时,分钟 if (DateTime.Now.Hour == executeTimer.Hour && DateTime.Now.Minute == executeTimer.Minute && DateTime.Now.Second == executeTimer.Second) { //执行线程数据 ExecuteHandleData(mThread); } } catch (Exception ex) { WriteExceptionLog(ex); mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; mThread.Message = "异常:" + ex.Message; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); Thread.Sleep(1000); #endregion } else if (mThread.TimerType == (int)TimerTypes.Month)//每月 { #region 每月 //定时时间是否异常 string[] arrTime = DataTypeConvert.ToArray(mThread.Time, '-');//日-小时:分钟 if (arrTime == null || arrTime.Length != 2) { mThread.Message = "执行时间(日-小时:分钟,如:1-00:30)异常!"; mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } DateTime executeTimer = DataTypeConvert.ToDateTime(DateTime.Now.ToString("yyyy-MM") + "-" + arrTime[0] + " " + arrTime[1]);//小时:分钟 if (executeTimer == DateTime.MinValue) { mThread.Message = "执行时间(日-小时:分钟,如:1-00:30)异常!"; mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } try { //如果停止则退出 if (mThread.Status == (int)ThreadStates.Stopped) { //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } //日,小时,分钟 if (DateTime.Now.Day == executeTimer.Day && DateTime.Now.Hour == executeTimer.Hour && DateTime.Now.Minute == executeTimer.Minute && DateTime.Now.Second == executeTimer.Second) { //执行线程数据 ExecuteHandleData(mThread); } } catch (Exception ex) { WriteExceptionLog(ex); mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; mThread.Message = "异常:" + ex.Message; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); Thread.Sleep(1000); #endregion } else if (mThread.TimerType == (int)TimerTypes.Week)//每周 { #region 每周 //定时时间是否异常 string[] arrTime = DataTypeConvert.ToArray(mThread.Time, '-');//星期-小时:分钟 if (arrTime == null || arrTime.Length != 2) { mThread.Message = "执行时间(星期-小时:分钟,如:星期日-00:30)异常!"; mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)]; DateTime executeTimer = DataTypeConvert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + arrTime[1]);//小时:分钟 if (!weekdays.Contains(arrTime[0]) || string.IsNullOrEmpty(week) || executeTimer == DateTime.MinValue) { mThread.Message = "执行时间(星期-小时:分钟,如:星期日-00:30)异常!"; mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } try { //如果停止则退出 if (mThread.Status == (int)ThreadStates.Stopped) { //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } //星期,小时,分钟 if (week == arrTime[0] && DateTime.Now.Hour == executeTimer.Hour && DateTime.Now.Minute == executeTimer.Minute && DateTime.Now.Second == executeTimer.Second) { //执行线程数据 ExecuteHandleData(mThread); } } catch (Exception ex) { WriteExceptionLog(ex); mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; mThread.Message = "异常:" + ex.Message; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); break; } //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); Thread.Sleep(1000); #endregion } else { #region 未知类型 mThread.Message = "定时执行类型未知!"; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); Thread.Sleep(1000); #endregion } #endregion } else { #region 未知类型 mThread.Message = "执行类型未知!"; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); Thread.Sleep(1000); #endregion } } } catch (Exception ex) { WriteExceptionLog(ex); mThread.StatusName = "停止"; mThread.Status = (int)ThreadStates.Stopped; mThread.Message = "异常:" + ex.Message; //ts = DateTime.Now - mThread.BeginTime; //mThread.RunTimes = DataTypeConvert.ToDateTime(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString("HH:mm:ss"); } }
/// <summary> /// 加载数据库中的线程配置到线程池中 /// </summary> /// <param name="connectionStringName">数据连接名称</param> /// <param name="dtData">线程集合</param> /// <returns></returns> public static List <SimpleThread> LoadThreadFromDatabase(string connectionStringName, DataTable dtData) { if (dtData == null || dtData.Rows.Count <= 0) { return(null); } string className = ""; string nameSpace = ""; Assembly assembly = null; int threadId = 0; //线程ID号 string threaNname = ""; //线程名称 string executeType = ""; string executeTypeText = ""; //执行类型枚举(持续执行或定时执行) string timerType = ""; //定时执行类型(每天、每月...) object obj = null; //线程执行对象 System.Threading.ThreadStart threadStar = null; System.Threading.Thread mThread = null; SimpleThread mSimpleThread = null; List <SimpleThread> list = new List <SimpleThread>(); foreach (DataRow row in dtData.Rows) { #region 创建执行实例 if (row["Class"] == null || row["Class"] + "" == "") { continue; } className = row["Class"] + ""; //反射创建线程执行实例 nameSpace = className.Substring(0, className.LastIndexOf(".")); assembly = Assembly.Load(nameSpace); obj = assembly.CreateInstance(className); IThreadExecuteService target = obj as IThreadExecuteService;//线程执行实例 if (target == null) { continue; } #endregion //创建线程 threaNname = (row["Code"] == null || row["Code"] + "" == "") ? className.Substring(className.LastIndexOf(".") + 1) : row["Name"] + ""; //threadId = AddThread(threaNname);//添加到线程池 //简单线程描述 mSimpleThread = AddThreadObject(threaNname); //添加到线程池 threadId = mSimpleThread.ID; mSimpleThread.ConnectionStringName = connectionStringName; //多个数据库情况 #region 执行实例赋值 //线程编号 mSimpleThread.ThreadCode = (row["Code"] == null || row["Code"] + "" == "") ? className.Substring(className.LastIndexOf(".") + 1) : row["Code"] + ""; //执行类型 executeType = (row["ExecuteType"] == null || row["ExecuteType"] + "" == "") ? "Continue" : row["ExecuteType"] + ""; mSimpleThread.ExecuteType = (int)ExecuteTypes.Continue; executeTypeText = "持续"; if (executeType == "Timer") { mSimpleThread.ExecuteType = (int)ExecuteTypes.Timer; executeTypeText = "定时"; } //定时类型 timerType = (row["TimerType"] == null || row["TimerType"] + "" == "") ? "Day" : row["TimerType"] + ""; if (timerType == "Day") { mSimpleThread.TimerType = (int)TimerTypes.Day; } else if (timerType == "Month") { mSimpleThread.TimerType = (int)TimerTypes.Month; } else if (timerType == "Week") { mSimpleThread.TimerType = (int)TimerTypes.Week; } if (mSimpleThread.ExecuteType == (int)ExecuteTypes.Timer) { //定时是每天还是每月 if (mSimpleThread.TimerType == (int)TimerTypes.Day) { executeTypeText += " 每天"; } else if (mSimpleThread.TimerType == (int)TimerTypes.Month) { executeTypeText += " 每月"; } else if (mSimpleThread.TimerType == (int)TimerTypes.Week) { executeTypeText += " 每周"; } } //执行或等待时间 mSimpleThread.Time = (row["Time"] == null || row["Time"] + "" == "") ? "86400" : row["Time"] + ""; if (mSimpleThread.ExecuteType == (int)ExecuteTypes.Continue) { executeTypeText += " 每隔" + mSimpleThread.Time + "秒执行"; } else if (mSimpleThread.ExecuteType == (int)ExecuteTypes.Timer) { if (mSimpleThread.TimerType == (int)TimerTypes.Day) { executeTypeText += mSimpleThread.Time + "执行"; } else if (mSimpleThread.TimerType == (int)TimerTypes.Month) { string[] arrTime = mSimpleThread.Time.Split('-'); if (arrTime != null && arrTime.Length == 2) { executeTypeText += arrTime[0] + "号" + arrTime[1] + "执行"; } } else if (mSimpleThread.TimerType == (int)TimerTypes.Week) { string[] arrTime = mSimpleThread.Time.Split('-'); if (arrTime != null && arrTime.Length == 2) { executeTypeText += arrTime[0] + "" + arrTime[1] + "执行"; } } } #endregion threadStar = new System.Threading.ThreadStart(target.Execute); mThread = new System.Threading.Thread(threadStar);//线程执行方法 mThread.Priority = System.Threading.ThreadPriority.Lowest; mThread.IsBackground = true; mSimpleThread.ThreadType = executeTypeText; mSimpleThread.ObjThread = mThread; mSimpleThread.ObjThreadExecuteService = target; //更新线程执行实例的线程ID target.ThreadID = threadId; //线程启动 mThread.Start(); list.Add(mSimpleThread); } return(list); }
/// <summary> /// 加载xml文件中的线程配置到线程池中 /// </summary> /// <param name="xmlFileName">xml文件的全路径(包含文件名称)</param> public static List <SimpleThread> LoadThreadFromXml(string xmlFileName) { if (!File.Exists(xmlFileName)) { return(null); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); string xpath = @"configuration/MultiThread"; XmlNode node = xmlDoc.SelectSingleNode(xpath); XmlNodeList nodeList = node.ChildNodes; if (nodeList == null || nodeList.Count <= 0) { return(null); } string className = ""; string nameSpace = ""; Assembly assembly = null; int threadId = 0; //线程ID号 string threaNname = ""; //线程名称 string executeType = ""; string executeTypeText = ""; //执行类型枚举(持续执行或定时执行) string timerType = ""; //定时执行类型(每天、每月...) object obj = null; //线程执行对象 System.Threading.ThreadStart threadStar = null; System.Threading.Thread mThread = null; SimpleThread mSimpleThread = null; foreach (XmlNode item in nodeList) { #region 创建执行实例 if (item.Attributes["Class"] == null) { continue; } className = item.Attributes["Class"].Value; if (string.IsNullOrEmpty(className)) { continue; } //反射创建线程执行实例 nameSpace = className.Substring(0, className.LastIndexOf(".")); assembly = Assembly.Load(nameSpace); obj = assembly.CreateInstance(className); IThreadExecuteService target = obj as IThreadExecuteService;//线程执行实例 if (target == null) { continue; } #endregion //创建线程 threaNname = (item.Attributes["Code"] == null || item.Attributes["Code"].Value == "") ? className.Substring(className.LastIndexOf(".") + 1) : item.Attributes["Code"].Value; //threadId = AddThread(threaNname);//添加到线程池 //简单线程描述 mSimpleThread = AddThreadObject(threaNname);//添加到线程池 threadId = mSimpleThread.ID; #region 执行实例赋值 //线程编号 mSimpleThread.ThreadCode = (item.Attributes["Code"] == null || item.Attributes["Code"].Value == "") ? className.Substring(className.LastIndexOf(".") + 1) : item.Attributes["Code"].Value; //执行类型 executeType = (item.Attributes["ExecuteType"] == null) ? "Continue" : item.Attributes["ExecuteType"].Value; mSimpleThread.ExecuteType = (int)ExecuteTypes.Continue; executeTypeText = "持续"; if (executeType == "Timer") { mSimpleThread.ExecuteType = (int)ExecuteTypes.Timer; executeTypeText = "定时"; } //定时类型 timerType = (item.Attributes["TimerType"] == null) ? "Day" : item.Attributes["TimerType"].Value; if (timerType == "Day") { mSimpleThread.TimerType = (int)TimerTypes.Day; } else if (timerType == "Month") { mSimpleThread.TimerType = (int)TimerTypes.Month; } else if (timerType == "Week") { mSimpleThread.TimerType = (int)TimerTypes.Week; } if (mSimpleThread.ExecuteType == (int)ExecuteTypes.Timer) { //定时是每天还是每月 if (mSimpleThread.TimerType == (int)TimerTypes.Day) { executeTypeText += " 每天"; } else if (mSimpleThread.TimerType == (int)TimerTypes.Month) { executeTypeText += " 每月"; } else if (mSimpleThread.TimerType == (int)TimerTypes.Week) { executeTypeText += " 每周"; } } //执行或等待时间 mSimpleThread.Time = (item.Attributes["Time"] == null) ? "86400" : item.Attributes["Time"].Value; if (mSimpleThread.ExecuteType == (int)ExecuteTypes.Continue) { executeTypeText += " 每隔" + mSimpleThread.Time + "秒执行"; } else if (mSimpleThread.ExecuteType == (int)ExecuteTypes.Timer) { if (mSimpleThread.TimerType == (int)TimerTypes.Day) { executeTypeText += mSimpleThread.Time + "执行"; } else if (mSimpleThread.TimerType == (int)TimerTypes.Month) { string[] arrTime = mSimpleThread.Time.Split('-'); if (arrTime != null && arrTime.Length == 2) { executeTypeText += arrTime[0] + "号" + arrTime[1] + "执行"; } } else if (mSimpleThread.TimerType == (int)TimerTypes.Week) { string[] arrTime = mSimpleThread.Time.Split('-'); if (arrTime != null && arrTime.Length == 2) { executeTypeText += arrTime[0] + "" + arrTime[1] + "执行"; } } } #endregion threadStar = new System.Threading.ThreadStart(target.Execute); mThread = new System.Threading.Thread(threadStar);//线程执行方法 mThread.Priority = System.Threading.ThreadPriority.Lowest; mThread.IsBackground = true; mSimpleThread.ThreadType = executeTypeText; mSimpleThread.ObjThread = mThread; mSimpleThread.ObjThreadExecuteService = target; //更新线程执行实例的线程ID target.ThreadID = threadId; //线程启动 mThread.Start(); } return(ThreadList); }