private bool UpdateDefintionFromWTS() { ScheduledJobTrigger current = null; ScheduledJobTrigger item = null; bool valueOrDefault = false; bool flag = false; using (ScheduledJobWTS scheduledJobWT = new ScheduledJobWTS()) { bool taskEnabled = scheduledJobWT.GetTaskEnabled(this._name); ScheduledJobOptions jobOptions = scheduledJobWT.GetJobOptions(this._name); Collection<ScheduledJobTrigger> jobTriggers = scheduledJobWT.GetJobTriggers(this._name); if (taskEnabled != this._enabled) { this._enabled = taskEnabled; flag = true; } if (jobOptions.DoNotAllowDemandStart != this._options.DoNotAllowDemandStart || jobOptions.IdleDuration != this._options.IdleDuration || jobOptions.IdleTimeout != this._options.IdleTimeout || jobOptions.MultipleInstancePolicy != this._options.MultipleInstancePolicy || jobOptions.RestartOnIdleResume != this._options.RestartOnIdleResume || jobOptions.RunElevated != this._options.RunElevated || jobOptions.RunWithoutNetwork != this._options.RunWithoutNetwork || jobOptions.ShowInTaskScheduler != this._options.ShowInTaskScheduler || jobOptions.StartIfNotIdle != this._options.StartIfNotIdle || jobOptions.StartIfOnBatteries != this._options.StartIfOnBatteries || jobOptions.StopIfGoingOffIdle != this._options.StopIfGoingOffIdle || jobOptions.StopIfGoingOnBatteries != this._options.StopIfGoingOnBatteries || jobOptions.WakeToRun != this._options.WakeToRun) { jobOptions.JobDefinition = this._options.JobDefinition; this._options = jobOptions; flag = true; } if (this._triggers.Count == jobTriggers.Count) { bool flag1 = false; IEnumerator<ScheduledJobTrigger> enumerator = jobTriggers.GetEnumerator(); using (enumerator) { do { if (!enumerator.MoveNext()) { continue; } current = enumerator.Current; if (this._triggers.ContainsKey(current.Id)) { item = this._triggers[current.Id]; if (item.DaysOfWeek != current.DaysOfWeek || item.Enabled != current.Enabled || item.Frequency != current.Frequency || item.Interval != current.Interval || item.RandomDelay != current.RandomDelay) { break; } DateTime? at = item.At; DateTime? nullable = current.At; if (at.HasValue != nullable.HasValue) { valueOrDefault = true; } else { if (!at.HasValue) { valueOrDefault = false; } else { valueOrDefault = at.GetValueOrDefault() != nullable.GetValueOrDefault(); } } } else { flag1 = true; break; } } while (!valueOrDefault && !(item.User != current.User)); flag1 = true; } if (flag1) { this.SetTriggers(jobTriggers, false); flag = true; } } else { this.SetTriggers(jobTriggers, false); flag = true; } } return flag; }
/// <summary> /// Compares the current ScheduledJobDefinition task scheduler information /// with the corresponding information stored in Task Scheduler. If the /// information is different then the task scheduler information in this /// object is updated to match what is in Task Scheduler, since that information /// takes precedence. /// /// Task Scheduler information: /// - Triggers /// - Options /// - Enabled state /// </summary> /// <returns>Boolean if this object data is modified.</returns> private bool UpdateDefintionFromWTS() { bool dataModified = false; // Get information from Task Scheduler. using (ScheduledJobWTS taskScheduler = new ScheduledJobWTS()) { bool wtsEnabled = taskScheduler.GetTaskEnabled(_name); ScheduledJobOptions wtsOptions = taskScheduler.GetJobOptions(_name); Collection<ScheduledJobTrigger> wtsTriggers = taskScheduler.GetJobTriggers(_name); // // Compare with existing object data and modify if necessary. // // Enabled. if (wtsEnabled != _enabled) { _enabled = wtsEnabled; dataModified = true; } // Options. if (wtsOptions.DoNotAllowDemandStart != _options.DoNotAllowDemandStart || wtsOptions.IdleDuration != _options.IdleDuration || wtsOptions.IdleTimeout != _options.IdleTimeout || wtsOptions.MultipleInstancePolicy != _options.MultipleInstancePolicy || wtsOptions.RestartOnIdleResume != _options.RestartOnIdleResume || wtsOptions.RunElevated != _options.RunElevated || wtsOptions.RunWithoutNetwork != _options.RunWithoutNetwork || wtsOptions.ShowInTaskScheduler != _options.ShowInTaskScheduler || wtsOptions.StartIfNotIdle != _options.StartIfNotIdle || wtsOptions.StartIfOnBatteries != _options.StartIfOnBatteries || wtsOptions.StopIfGoingOffIdle != _options.StopIfGoingOffIdle || wtsOptions.StopIfGoingOnBatteries != _options.StopIfGoingOnBatteries || wtsOptions.WakeToRun != _options.WakeToRun) { // Keep the current scheduled job definition reference. wtsOptions.JobDefinition = _options.JobDefinition; _options = wtsOptions; dataModified = true; } // Triggers. if (_triggers.Count != wtsTriggers.Count) { SetTriggers(wtsTriggers, false); dataModified = true; } else { bool foundTriggerDiff = false; // Compare each trigger object. foreach (var wtsTrigger in wtsTriggers) { if (_triggers.ContainsKey(wtsTrigger.Id) == false) { foundTriggerDiff = true; break; } ScheduledJobTrigger trigger = _triggers[wtsTrigger.Id]; if (trigger.DaysOfWeek != wtsTrigger.DaysOfWeek || trigger.Enabled != wtsTrigger.Enabled || trigger.Frequency != wtsTrigger.Frequency || trigger.Interval != wtsTrigger.Interval || trigger.RandomDelay != wtsTrigger.RandomDelay || trigger.At != wtsTrigger.At || trigger.User != wtsTrigger.User) { foundTriggerDiff = true; break; } } if (foundTriggerDiff) { SetTriggers(wtsTriggers, false); dataModified = true; } } } return dataModified; }