c# 定点、定时执行任务

public class TaskTime
    {
        private static readonly BLL bllBase = null;
        static TaskTime()
        {
            bllBase = new BLL();
        }
        private static Timer _timer;
        public static void StartTask()
        {
            #region MyRegion

            
            #endregion

            try
            {
                if (!string.IsNullOrEmpty(Common.Constants.IsUserIntervalMinute) && Common.Constants.IsUserIntervalMinute == "1")
                {
                    _timer = new Timer(CalculationFundsTask, null, 0, Timeout.Infinite);//5分钟一次
                }
                else//定点执行
                {
                    //服务启动就执行计算

                    TimerCallback timerDelegate = new TimerCallback(CalculationFundsTask);
                    DateTime dtS = DateTime.Parse(GetRevenueComputeTime());   //每天晚上 23:59 运行
                    long span = (dtS - DateTime.Now).Ticks;
                    if (span < 0)
                        dtS = dtS.AddDays(1);
                    TimeSpan ts1 = new TimeSpan(dtS.Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2);
                    _timer = new Timer(timerDelegate, null, ts, TimeSpan.FromSeconds(0));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("StartTask方法产生异常:", ex);
                ExceptionNotice.Notice(ex);
            }
        }
        private static string GetRevenueComputeTime()
        {
            string configDate = "6"string tempDate = string.Empty;
            if (configDate.IndexOf(':') == -1)
            {
                Int32 intconfigDate = 0;
                if (Int32.TryParse(configDate, out intconfigDate))
                {
                    if (intconfigDate < 10)
                    {
                        tempDate = "0" + intconfigDate.ToString() + ":00";
                    }
                    else
                    {
                        tempDate = intconfigDate.ToString() + ":00";
                    }
                }
                else
                {
                    LogHelper.WriteLog("时间要为整数:" + configDate);
                }
            }
            else
                tempDate = configDate;
            LogHelper.WriteLog("每天执行时间:" + tempDate);
            return tempDate;
        }
        private static void CalculationFundsTask(object state)
        {
            try
            {
                //执行你的任务方法
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("CalculUserFunds方法产生异常:", ex);
            }
            if (!string.IsNullOrEmpty(Common.Constants.IsUserIntervalMinute) && Common.Constants.IsUserIntervalMinute == "1")
            {
                _timer.Change(Common.Constants.IntervalMinute, Timeout.Infinite);//5分钟一次
            }
            else
            {
                long timeCount = 1000 * 60 * 60 * 24;
                _timer.Change(timeCount, Timeout.Infinite);//每隔 24小时的 启动线程
            }
           
        }
    }

 

posted @ 2014-08-07 10:13  英雄饶命啊  阅读(1027)  评论(0)    收藏  举报