コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }