Example #1
0
        /// <summary>
        /// Schedules the task for later execution. Long-blocking tasks should never be run
        /// synchronously, as any and all tasks that can be executed will be executed each cycle.
        /// Additionally, the scheduler does not guarantee the timing of each task. If you need
        /// precision timing, look elsewhere.
        /// <para>
        /// If task.Task is null, the task will not be added. If task.Completed is true, the task will be
        /// added but will be removed if task.Completed is not set to false by the next call of RunTasks.
        /// </para>
        /// </summary>
        /// <param name="task">The task info object.</param>
        public void ScheduleTask(TaskInfo task)
        {
            task.Completed = false;

            if (task.Task == null)
            {
                Log.Warn("skipping null task; setting Completed to true and skipping");
                task.Completed = true;
            }

            task.TimeLeft = task.ExecuteAfter;

            lock (this.taskLock)
            {
                this.Tasks.AddLast(task);
            }
        }
Example #2
0
 /// <summary>
 /// Removes the task from the scheduler.
 /// </summary>
 /// <param name="task">The task to remove.</param>
 /// <returns>true if the task was removed, false if the task was not scheduled.</returns>
 public bool RemoveTask(TaskInfo task)
 {
     lock (this.taskLock)
     {
         return this.Tasks.Remove(task);
     }
 }
Example #3
0
        public static void Timer(ConsoleManager console, ExecutableCommand cmd)
        {
            if (cmd.Arguments.Count != 2)
            {
                throw new ArgumentException(string.Format("Wrong number of arguments for timer (expected 2, got {0})", cmd.Arguments.Count));
            }

            string secondsString = cmd.Arguments[0].Value;
            float seconds = 0;
            if (!float.TryParse(secondsString, out seconds))
            {
                throw new ArgumentException(string.Format("Unable to parse \"{0}\" to a float", secondsString));
            }

            string command = cmd.Arguments[1].Value;

            TaskInfo task = new TaskInfo()
            {
                ExecuteAfter = seconds,
                Task = () =>
                    {
                        try
                        {
                            console.Execute(ScriptUtilities.ParseString(command));
                        }
                        catch (Exception e)
                        {
                            ConsoleManager.ConsoleLog.Warn("Unable to execute delayed commands", e);
                        }
                    }
            };

            GameEngine.Instance.Scheduler.ScheduleTask(task);
        }