/// <summary>
        /// Cancels the given scheduled task.
        /// </summary>
        /// <param name="scheduledTask">The scheduled task to cancel.</param>
        public void CancelAction(ScheduledTask scheduledTask)
        {
            lock (this.AccessLock)
            {
                ScheduledTaskManager scheduledTaskManager = this.Actions.Select(x => x).Where(x => x.ScheduledTask == scheduledTask).FirstOrDefault();

                if (scheduledTaskManager == null)
                {
                    return;
                }

                scheduledTaskManager.Cancel();
            }
        }
Example #2
0
        static void Main()
        {
            var configService        = new Services.DefaultScheduledTaskConfigService("ScheduledTasks.xml");
            var pluginManagerService = new Services.DefaultScheduledTaskPluginManagerService(configService);
            var scheduledTaskManager = new ScheduledTaskManager(pluginManagerService);

            scheduledTaskManager.Start();

            Console.WriteLine("ScheduledTaskManager is running.");
            Console.WriteLine("Press any key to exit.");

            Console.ReadKey();

            scheduledTaskManager.Stop();
        }
 /// <summary>
 /// 定时获取比赛信息
 /// </summary>
 /// <returns>异步任务</returns>
 public static void ScheduleGetAllRecentContestsJsonFromWeb()
 {
     ScheduledTaskManager.Schedule("RequestRecentContest", 0, AUTO_REQUEST_INTERVAL,
                                   RecentContestManager.ScheduleGetAllRecentContestsJsonCallback);
 }
        /// <summary>
        /// The scheduler update loop. Cycles through tasks, updating them.
        /// </summary>
        private void Update()
        {
            Task.Run(
                async() =>
            {
                do
                {
                    await Task.Delay(ActionSchedulerViewModel.SchedulerInterval);

                    lock (this.AccessLock)
                    {
                        // Check for a change in the active tasks
                        if (!this.Actions.Select(x => x.ScheduledTask).SequenceEqual(this.ActiveTasks))
                        {
                            this.RaisePropertyChanged(nameof(this.ActiveTasks));
                        }

                        // Cycle to the next task
                        this.NextAction = this.NextAction?.NextOrFirst() ?? this.Actions.First;

                        if (NextAction == null)
                        {
                            continue;
                        }

                        ScheduledTaskManager nextTask = this.NextAction.Value;

                        if (nextTask.CanStart)
                        {
                            // Check if dependencies are complete for this task to start
                            if (nextTask.ScheduledTask.DependencyBehavior.IsDependencyRequiredForStart &&
                                !this.DependenciesResolved(nextTask.ScheduledTask))
                            {
                                continue;
                            }

                            // Start the task
                            nextTask.InitializeStart();
                            Task.Run(() => nextTask.StartAction());
                        }
                        else if (nextTask.CanUpdate)
                        {
                            // Check if dependencies are complete for this task to update
                            if (nextTask.ScheduledTask.DependencyBehavior.IsDependencyRequiredForUpdate &&
                                !this.DependenciesResolved(nextTask.ScheduledTask))
                            {
                                continue;
                            }

                            // Update the task
                            nextTask.InitializeUpdate();
                            Task.Run(() => nextTask.UpdateAction());
                        }
                        else if (nextTask.CanEnd)
                        {
                            // End the task
                            Task.Run(() => nextTask.EndAction());

                            // Permanently remove this task
                            this.Actions.Remove(nextTask);
                            this.RaisePropertyChanged(nameof(this.ActiveTasks));
                        }
                    }
                }while (true);
            });
        }