/// <summary> /// Executes a job on demand, rather than waiting for its regularly scheduled time. /// </summary> /// <param name="job">The job to be executed.</param> public static void ExecuteJob(JobBase job) { ReaderWriterLock rwLock = new ReaderWriterLock(); try { rwLock.AcquireReaderLock(Timeout.Infinite); if (job.Executing == false) { LockCookie lockCookie = rwLock.UpgradeToWriterLock(Timeout.Infinite); try { if (job.Executing == false) { job.Executing = true; QueueJob(job); } } finally { rwLock.DowngradeFromWriterLock(ref lockCookie); } } } finally { rwLock.ReleaseReaderLock(); } }
/// <summary> /// Method to be called after an execption occurs during job execution. /// </summary> /// <param name="job">The job that was executed.</param> /// <param name="e">The exception that occurred.</param> public void OnError(JobBase job, Exception e) { encounteredError = true; isDone = true; }
/// <summary> /// Method to be called after the job executes successfully. /// </summary> /// <param name="job">The job that was executed.</param> public void OnComplete(JobBase job) { isComplete = true; isDone = true; }
/// <summary> /// When overriden in a derived class, provides an entry point for the Scheduler to notify the trigger that a job has completed. /// </summary> /// <param name="job">The job that has completed.</param> /// <param name="e">The exception that was thrown during the job execution, if any; otherwise, <code>null</code>.</param> public abstract void JobCompleted(JobBase job, Exception e);
/// <summary> /// Queues a job to be executed on a new thread. /// </summary> /// <param name="job">The job to be executed.</param> private static void QueueJob(JobBase job) { if (queue.Contains(job) == false) { queue.Enqueue(job); Thread thread = new Thread(new ThreadStart(ExecuteJob)); thread.IsBackground = true; thread.Start(); } }