Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScheduleItem"/> class.
        /// </summary>
        /// <param name="setting">
        /// The setting.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <param name="isCreated">
        /// The is created.
        /// </param>
        /// <param name="isDirty">
        /// The is dirty.
        /// </param>
        /// <param name="createdName">
        /// The created name.
        /// </param>
        public ScheduleItem(SchedulerSetting setting, ExecutionParam parameters, bool isCreated, bool isDirty, string createdName)
        {
            this.Setting = setting;
            this.Parameters = parameters;
            if (this.Parameters.DepartureDate == DateTime.MinValue)
            {
                this.Parameters.DepartureDate = DateTime.Now.Date;
            }

            if (this.Parameters.ReturnDate == DateTime.MinValue)
            {
                this.Parameters.ReturnDate = this.Parameters.DepartureDate.Date.AddDays(this.Parameters.MaxStayDuration);
            }

            this.IsCreated = isCreated;
            this.CreatedName = createdName;
        }
Exemplo n.º 2
0
        /// <summary>The get scheduled tasks.</summary>
        /// <returns>The <see cref="List" />.</returns>
        public List<ScheduleItem> GetScheduledTasks()
        {
            var result = new List<ScheduleItem>();

            // Get the service on the local machine
            using (var ts = new TaskService())
            {
                TaskCollection tc = ts.RootFolder.GetTasks(null);
                foreach (Task task in tc)
                {
                    try
                    {
                        if (task.Definition.Actions.Count > 0)
                        {
                            var action = task.Definition.Actions[0] as ExecAction;
                            if (action != null)
                            {
                                if (string.Compare(
                                    CurrentExe,
                                    Path.GetFullPath(action.Path.Replace("\"", string.Empty)),
                                    StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    try
                                    {
                                        ExecutionParam param = null;
                                        try
                                        {
                                            ExecutionParam.Parse(action.Arguments.SplitCommandLine(), null, this._logger, out param);
                                        }
                                        catch
                                        {
                                            param = new ExecutionParam();
                                        }

                                        DateTime activeTriggerDate = DateTime.MinValue;
                                        foreach (Trigger trigger in task.Definition.Triggers)
                                        {
                                            var timeTrigger = trigger as DailyTrigger;
                                            if (timeTrigger != null)
                                            {
                                                activeTriggerDate = timeTrigger.StartBoundary;
                                                break;
                                            }
                                        }

                                        var setting = new SchedulerSetting
                                                          {
                                                              TaskName = task.Name.Replace(AppUtil.ProductName, string.Empty),
                                                              IsEnabled = task.Enabled,
                                                              ScheduledStartTime = activeTriggerDate
                                                          };
                                        result.Add(new ScheduleItem(setting, param, true, false, task.Name));
                                    }
                                    catch (Exception ex)
                                    {
                                        this._logger.Warn("Failed to get scheduled tasks: " + ex.Message);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this._logger.ErrorFormat("Could not load task [{0}]: {1}", task.Name, ex.Message);
                    }
                }
            }

            return result;
        }