This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Epc | |
{ | |
public abstract class Task | |
{ | |
public abstract string TaskName { get; } | |
public DateTime? WhenLastRun { get; private set; } | |
public abstract int? IntervalInSeconds { get; } | |
public abstract DateTime? TimeToRun { get; } | |
public bool IsBusy { get; private set; } | |
public TaskHost Host = null; | |
public System.ComponentModel.BackgroundWorker Worker = null; | |
public virtual LogLevels LogLevel | |
{ | |
get | |
{ | |
return LogLevels.Service; | |
} | |
} | |
public abstract void DoWork(); | |
public void LogEvent(LogLevels logLevel, string text) | |
{ | |
if (this.Host != null) | |
{ | |
this.Host.LogEvent(this, logLevel, text); | |
} | |
} | |
public void LogException(Exception ex) | |
{ | |
if (this.Host != null) | |
{ | |
this.LogEvent(LogLevels.Debug, "Logged Exception: {0}".FormatWith(ex.Message)); | |
this.Host.LogException(this, ex); | |
} | |
} | |
public void Start(TaskHost host) | |
{ | |
try | |
{ | |
this.Host = host; | |
LogEvent(LogLevels.Debug, "In Start."); | |
if (!this.IntervalInSeconds.HasValue && !this.TimeToRun.HasValue) | |
{ | |
throw new SanityCheckException("You can't have IntervalInSeconds and TimeToRun both set, one of them should return null."); | |
} | |
if (this.IsBusy) | |
{ | |
LogEvent(LogLevels.Debug, "Task is currently busy, skipping processing this time around."); | |
} | |
else | |
{ | |
if (this.Host.ForceRun | |
|| (this.IntervalInSeconds.HasValue && (!this.WhenLastRun.HasValue || this.WhenLastRun.Value.AddSeconds(this.IntervalInSeconds.ToInt32()) <= DateTime.Now)) | |
//Verify that time to run matches hour and minute, also make sure we haven't just run this process within the last minute | |
|| (this.TimeToRun.HasValue && TimeToRun.Value.Hour == DateTime.Now.Hour && TimeToRun.Value.Minute == DateTime.Now.Minute && (!this.WhenLastRun.HasValue || DateTime.Now > this.WhenLastRun.Value.AddSeconds(60))) | |
) | |
{ | |
if (this.WhenLastRun.HasValue) | |
{ | |
LogEvent(LogLevels.Debug, "When Last Run: " + this.WhenLastRun.Value.ToString()); | |
} | |
LogEvent(LogLevels.Debug, "Preparing to DoWork."); | |
IsBusy = true; | |
this.Worker = new System.ComponentModel.BackgroundWorker(); | |
this.Worker.DoWork += new System.ComponentModel.DoWorkEventHandler(Worker_DoWork); | |
this.Worker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted); | |
this.Worker.Disposed += new EventHandler(Worker_Disposed); | |
if (!this.Worker.IsBusy) | |
{ | |
this.Worker.RunWorkerAsync(); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
this.LogException(ex); | |
this.IsBusy = false; | |
} | |
LogEvent(LogLevels.Debug, "Out Start."); | |
} | |
void Worker_Disposed(object sender, EventArgs e) | |
{ | |
this.IsBusy = false; | |
} | |
public void ReportProgress(int progressPercent) | |
{ | |
try | |
{ | |
if (this.Worker != null) | |
{ | |
this.Worker.WorkerReportsProgress = true; | |
this.Worker.ReportProgress(progressPercent); | |
LogEvent(LogLevels.Debug, "Progress Reported: {0}".FormatWith(progressPercent)); | |
} | |
} | |
catch (Exception ex) | |
{ | |
LogException(ex); | |
this.IsBusy = false; | |
} | |
} | |
void Worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) | |
{ | |
LogEvent(LogLevels.Debug, "In Worker_RunWorkerCompleted."); | |
this.WhenLastRun = DateTime.Now; | |
this.IsBusy = false; | |
if (e.Error != null) | |
{ | |
this.LogException(e.Error); | |
} | |
try | |
{ | |
this.Worker.Dispose(); | |
this.Worker = null; | |
} | |
catch | |
{ | |
this.IsBusy = false; | |
} | |
LogEvent(LogLevels.Debug, "Out Worker_RunWorkerCompleted."); | |
} | |
void Worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) | |
{ | |
} | |
void Worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) | |
{ | |
LogEvent(LogLevels.Debug, "In Worker_DoWork."); | |
try | |
{ | |
LogEvent(LogLevels.Debug, "In DoWork."); | |
this.DoWork(); | |
LogEvent(LogLevels.Debug, "Out DoWork."); | |
this.IsBusy = false; | |
} | |
catch (Exception ex) | |
{ | |
this.IsBusy = false; | |
this.Host.LogException(this, ex); | |
} | |
LogEvent(LogLevels.Debug, "Out Worker_DoWork."); | |
} | |
public void Stop() | |
{ | |
LogEvent(LogLevels.Debug, "In Stop."); | |
while (IsBusy) | |
{ | |
System.Threading.Thread.Sleep(1); | |
} | |
LogEvent(LogLevels.Debug, "Out Start."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment