示例#1
0
        /// <summary>
        /// 执行单个任务
        /// </summary>
        /// <param name="task"></param>
        private static void ExecuteTask(TaskBase task)
        {
            //设置最后一次执行时间
            task.LastExecuteTime = DateTime.Now;
            RecordExecuteTime(task.Type, DateTime.Now);

            //Log4Helper.Info("task:" + task.TaskName + ",type:" + task.Type + ",time:" + DateTime.Now.ToString());
            ManagedThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(task.Execute));

            //  task.Execute(null);
        }
示例#2
0
        /// <summary>
        /// 检查任务是否到了可以执行的时间
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        private static bool IsExecuteTime(TaskBase task)
        {
            DateTime executeTime;

            switch (task.TimeType)
            {
            case TaskTimeType.Interval:
                if (task.LastExecuteTime.AddSeconds(task.ExecuteTime.TimeSinceLastEvent.TotalSeconds) <= DateTime.Now)
                {
                    //Log4Helper.Info("预计执行时间:" + task.LastExecuteTime.AddSeconds(task.Interval).ToString());
                    // if (task.LastExecuteTime.AddTicks(task.Interval) <= DateTime.Now)
                    return(true);
                }
                break;

            case TaskTimeType.Seconds:
                if (task.LastExecuteTime.AddSeconds(task.Interval) <= DateTime.Now)
                {
                    //Log4Helper.Info("预计执行时间:" + task.LastExecuteTime.AddSeconds(task.Interval).ToString());
                    // if (task.LastExecuteTime.AddTicks(task.Interval) <= DateTime.Now)
                    return(true);
                }
                break;

            case TaskTimeType.Hour:
                // executeTime = new DateTime(task.LastExecuteTime.Year, task.LastExecuteTime.Month,task.LastExecuteTime.Day, task.LastExecuteTime.Hour, task.ExecuteTime.TimeSinceLastEvent.Minutes, task.ExecuteTime.TimeSinceLastEvent.Seconds);
                executeTime = task.LastExecuteTime;
                // Log4Helper.Info("按小时间隔执行任务“"+task.TaskName+"”,预计下次执行时间:" + executeTime.AddHours(Convert.ToInt32(task.Interval)).ToString());
                if (executeTime.AddHours(Convert.ToInt32(task.Interval)) <= DateTime.Now)
                {
                    return(true);
                }
                break;

            case TaskTimeType.Day:
                //executeTime = new DateTime(task.LastExecuteTime.Year, task.LastExecuteTime.Month, task.LastExecuteTime.Day, task.ExecuteTime.TimeSinceLastEvent.Hours, task.ExecuteTime.TimeSinceLastEvent.Minutes, task.ExecuteTime.TimeSinceLastEvent.Seconds);
                executeTime = task.LastExecuteTime;
                // Log4Helper.Info("按天间隔执行任务“" + task.TaskName + "”,预计下次执行时间:" + executeTime.AddDays(Convert.ToInt32(task.Interval)).ToString());
                if (executeTime.AddDays(Convert.ToInt32(task.Interval)) < DateTime.Now)
                {
                    return(true);
                }
                break;

            case TaskTimeType.Week:
                if (DateTime.Now.DayOfWeek == task.DayOfWeek)
                {
                    if (task.LastExecuteTime.Year == DateTime.Now.Year && task.LastExecuteTime.Month == DateTime.Now.Month && task.LastExecuteTime.Day == DateTime.Now.Day)
                    {
                    }
                    else
                    {
                        // executeTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, task.ExecuteTime.TimeSinceLastEvent.Hours, task.ExecuteTime.TimeSinceLastEvent.Minutes, task.ExecuteTime.TimeSinceLastEvent.Seconds);
                        executeTime = task.LastExecuteTime;
                        if (executeTime.AddDays(Convert.ToInt32(task.Interval) * 7) <= DateTime.Now)
                        {
                            return(true);
                        }
                    }
                }
                break;

            case TaskTimeType.Month:
                // executeTime = new DateTime(task.LastExecuteTime.Year, task.LastExecuteTime.Month, task.ExecuteTime.TimeSinceLastEvent.Days, task.ExecuteTime.TimeSinceLastEvent.Hours, task.ExecuteTime.TimeSinceLastEvent.Minutes, task.ExecuteTime.TimeSinceLastEvent.Seconds);
                executeTime = task.LastExecuteTime;
                if (executeTime.AddMonths(Convert.ToInt32(task.Interval)) < DateTime.Now)
                {
                    return(true);
                }
                break;

            case TaskTimeType.Year:
                //executeTime = new DateTime(task.LastExecuteTime.Year, task.Month, task.ExecuteTime.TimeSinceLastEvent.Days, task.ExecuteTime.TimeSinceLastEvent.Hours, task.ExecuteTime.TimeSinceLastEvent.Minutes, task.ExecuteTime.TimeSinceLastEvent.Seconds);
                executeTime = task.LastExecuteTime;
                if (executeTime.AddYears(Convert.ToInt32(task.Interval)) < DateTime.Now)
                {
                    return(true);
                }
                break;
            }
            return(false);
        }