private static void RunAllBackgroundTasksOfType(CmsBackgroundTaskInfo.CmsTaskType typeOfTasksToRun)
 {
     CmsBackgroundTask[] allTasks = getAllBackgroundTasks();
     foreach (CmsBackgroundTask task in allTasks)
     {
         CmsBackgroundTaskInfo taskInfo = task.getBackgroundTaskInfo();
         if (taskInfo != null && taskInfo.TaskType == typeOfTasksToRun)
         {
             task.RunBackgroundTask();
         }
     } // foreach
 }
 public static void RunAllApplicablePeriodicTasks()
 {
     CmsBackgroundTask[] allTasks = getAllBackgroundTasks();
     foreach (CmsBackgroundTask task in allTasks)
     {
         CmsBackgroundTaskInfo taskInfo = task.getBackgroundTaskInfo();
         if (taskInfo != null && taskInfo.TaskType == CmsBackgroundTaskInfo.CmsTaskType.Periodic)
         {
             DateTime lastRunTime = GetLastPeriodicTaskStartTime(task);
             DateTime nextRunTime = GetNextRunTime(taskInfo, lastRunTime);
             DateTime now         = DateTime.Now;
             if (nextRunTime <= now)
             {
                 task.RunBackgroundTask();
                 SaveLastPeriodicTaskStartTime(task, now);
             }
         }
     } // foreach
 }
        /// <summary>
        /// </summary>
        /// <param name="taskinfo"></param>
        /// <param name="lastRunTime"></param>
        /// <returns></returns>
        private static DateTime GetNextRunTime(CmsBackgroundTaskInfo taskinfo, DateTime lastRunTime)
        {
            if (lastRunTime == DateTime.MinValue)
            {
                return(DateTime.MinValue);
            }

            switch (taskinfo.PeriodicTaskPeriod)
            {
            case CmsBackgroundTaskInfo.CmsPeriodicTaskPeriod.EveryDay:
                return(lastRunTime.AddDays(1));

                break;

            case CmsBackgroundTaskInfo.CmsPeriodicTaskPeriod.EveryHour:
                return(lastRunTime.AddHours(1));

                break;

            case CmsBackgroundTaskInfo.CmsPeriodicTaskPeriod.EveryTwoDays:
                return(lastRunTime.AddDays(2));

                break;

            case CmsBackgroundTaskInfo.CmsPeriodicTaskPeriod.EveryWeek:
                return(lastRunTime.AddDays(7));

                break;

            case CmsBackgroundTaskInfo.CmsPeriodicTaskPeriod._NotPeriodic:
                return(DateTime.MaxValue);

                break;

            default:
                throw new ArgumentException("Invalid CmsBackgroundTaskInfo.CmsPeriodicTaskPeriod specified");
                break;
            }
        }