/// <summary>
 /// Runs the specified task.
 /// </summary>
 /// <param name="task">The task.</param>
 private void Run(PlannedTask task)
 {
     log.Debug("Run task: " + task.Description);
     // run planned action
     task.Execute();
     log.Info("Executed task: " + task.Description);
     // Schedule next execution
     this.ScheduleTask(task);
 }
        /// <summary>
        /// Schedules the task.
        /// </summary>
        /// <param name="task">The task.</param>
        private void ScheduleTask(PlannedTask task)
        {
            TimeSpan ts;
            var now = DateTime.Now;
            if (task.StartDate > now)
                ts = task.StartDate - now;
            else
            {
                task.StartDate = task.StartDate.Add(task.Interval);
                ts = task.StartDate - now;
            }

            //waits certan time and run the code
            Task.Delay(ts).ContinueWith((x) => Run(task));
            log.Info(string.Format("Scheduled task: {0}     at: {1}", task.Description, task.StartDate));
        }
        /// <summary>
        /// Execute during start procedure.
        /// </summary>
        /// <param name="args">The args.</param>
        public virtual void PerformStartup(string[] args)
        {
            log.Debug("Reading parameters.");
            // Load application parameters.
            Config.CommandLineArguments = args;
            Config.Load();
            log.Debug("Reading parameters finished.");
            try
            {
                log.Info("Before Planning");
                var startDate = DateTime.Now.AddSeconds(10);
                var interval = new TimeSpan(24, 0, 0);
                log.Info("First occur: " + startDate.ToString());

                var task = new PlannedTask(startDate, interval, () => Download(7), "Task");

                TaskScheduler.Instance.AddTask(task);
                this.dateOfLastDownload = startDate;
            }
            catch (Exception e)
            {
                log.Error("Error :", e);
            }
        }
 /// <summary>
 /// Adds the task.
 /// </summary>
 /// <param name="task">The planned task.</param>
 public void AddTask(PlannedTask task)
 {
     this.ScheduleTask(task);
 }