小周的技术博客

困难是培养伟大心志的保姆,唯有这个冷酷的保姆才会不停地推着摇篮,培养一个勇敢、刚健的孩子。 ---------布赖恩特

C# System.Threading.Timer 使用方法

public class TimerHelper
    {
        System.Threading.Timer timer;
        public TaskSendMMS tasksendmms { get; set; }
        public void Start()
        {
            timer = new System.Threading.Timer(new TimerCallback(tasksendmms.Execute), null, 100, 30000);
            tasksendmms.timer = timer; 
        }
    }
public class TaskSendMMS
    {

        public Guid MMSId { get; set; }

        public Guid PhonePackageId { get; set; }

        public DateTime ExeTime { get; set; }

        private log log = new log();

        public System.Threading.Timer timer;

        public TaskSendMMS()
        {

        }
        public void Execute(object a)
        {
            try
            {
                log.writelog("=========================================开始执行计划========================================");
                string Extime = ExeTime.ToString("MM-dd HH:mm");
                string today = DateTime.Now.ToString("MM-dd HH:mm");
                if (today.Equals(Extime))//如果时间是执行时间,则执
                {
                    log.writelog("执行时间到了,时间为:" + today);
                    DataBase.DataClasses_MMSDataContext db = new DataBase.DataClasses_MMSDataContext();
                    var mms = db.MMS.Where(c => c.ID == this.MMSId).FirstOrDefault();
                    log.writelog("查询数据库MMS记录名称:" + mms.Title);
                    if (mms.State != 2) //如果该彩信 不是延迟发送的,直接退出.
                    {
                        log.writelog("该彩信不是延迟发送的,直接退出");
                        if (null != timer)
                        {
                            log.writelog("该彩信不是延迟发送的,直接退出 并关闭timer");
                            timer.Dispose();
                        }
                    }
                    else
                    {
                        log.writelog("执行时间到了,开始调用Web接口立即发送彩信.");
                        //发送彩信
                        Business.Start(this.MMSId.ToString(), this.PhonePackageId.ToString(), "");
                        log.writelog("调用接口成功,完成发送彩信.");
                        mms.State = 1;
                        db.SubmitChanges();
                        log.writelog("彩信状态,修改为1");
                        if (null != timer)
                        {
                            log.writelog("全部完成 并关闭timer");
                            timer.Dispose();
                        }
                    }
                }
                else
                {
                    log.writelog("执行时间未到! 时间为: " + today);
                }
            }
            catch (Exception ee)
            {
                log.writelog("出现异常信息:" + ee.Message);
            }
            log.writelog("=========================================结束执行计划========================================");
        }
    }
/// <summary>
        /// 启动执行计划 
        /// </summary>
        /// <param name="MmsId"></param>
        /// <param name="packageIDs"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public int StartSendMMs(string MmsId, string packageIDs, string datetime)
        {
            TimerHelper timer = new TimerHelper();
            timer.tasksendmms = new TaskSendMMS() { ExeTime = DateTime.Parse(datetime), MMSId = new Guid(MmsId), PhonePackageId = new Guid(packageIDs) };
            timer.Start();
            return 0;
        }


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。