Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Processor class for critical jobs in c#

View Processor.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
public interface IProcessor
{
void Process();
}
 
public abstract class BaseProcessor
: IProcessor
{
public delegate void ProcessorStartingEventHandler(params Object[] param);
public delegate void ProcessorStartedEventHandler(params Object[] param);
public delegate void ProcessorCompletedEventHandler(params Object[] param);
public delegate void ProcessorErrorEventHandler(params Object[] param);
 
public ProcessorStartingEventHandler ProcessorStarting = null;
public ProcessorStartedEventHandler ProcessorStarted = null;
public ProcessorCompletedEventHandler ProcessorCompleted = null;
public ProcessorErrorEventHandler ProcessorError = null;
 
// you can set status in the processor class
public ProcessorStatus.Current Status { get; private set; }
 
// parameter variables
public Parameters _parameters = null;
public Parameters Parameters
{
set { _parameters = value; }
}
 
// threading mechanism (Semaphore)
private static Semaphore _semaphore = null;
private static object _semaphoreLockObj = new object();
// singleton pattern for semaphore
private void InitializeSemaphore(int jobCountLimit)
{
if (_semaphore == null)
{
lock (_semaphoreLockObj)
{
if (_semaphore == null)
{
_semaphore = new Semaphore(jobCountLimit, jobCountLimit);
}
}
}
}
 
// ctor methods
public BaseProcessor(Parameters param, int jobCountLimit)
{
this._parameters = param;
InitializeSemaphore(jobCountLimit);
}
 
public BaseProcessor(Parameters param)
: this(param, 3)
{ }
 
// Real Job Method
protected abstract void RunJob(object param, out object[] output);
 
public void Process()
{
// starting loging for real job's log table
this.Status = ProcessorStatus.Current.Starting;
 
if (ProcessorStarting != null)
ProcessorStarting(this.GetType());
 
ThreadPool.QueueUserWorkItem(new WaitCallback(Job), this._parameters);
}
 
private void Job(object param)
{
object[] output = null;
 
// started loging
this.Status = ProcessorStatus.Current.Started;
if (ProcessorStarted != null)
ProcessorStarted(this.GetType());
 
try
{
_semaphore.WaitOne();
RunJob(param, out output);
}
catch (Exception ex)
{
this.Status = ProcessorStatus.Current.Error;
if (ProcessorError != null)
{
ProcessorError(this.GetType(), ex);
}
// error logging
}
finally
{
_semaphore.Release();
}
 
// completing loging
this.Status = ProcessorStatus.Current.Completing;
 
if (ProcessorCompleted != null)
{
ProcessorCompleted(this.GetType(), output);
}
 
// completed loging
this.Status = ProcessorStatus.Current.Completed;
}
}
 
// Customized Processor Class for custom jobs
public class MainProcessor
: BaseProcessor
{
public MainProcessor(Parameters param, int jobLimit)
: base(param, jobLimit)
{ }
 
public MainProcessor(Parameters param)
: base(param)
{ }
 
protected override void RunJob(object param, out object[] output)
{
MainJob mj = new MainJob();
output = mj.Start(param as Parameters);
}
}
 
public class ProcessorStatus
{
public enum Current
{
Starting = 1,
Started = 2,
Completing = 3,
Completed = 4,
Error = 5
}
}
 
public class Parameters
{
public IDictionary<string, Object> _prms = null;
 
public Parameters()
{
this._prms = new Dictionary<string, Object>();
}
 
 
public void Add(string key, object value)
{
this.Remove(key);
 
if (!String.IsNullOrEmpty(key))
this._prms.Add(key, value);
}
 
public void Remove(string key)
{
if (this._prms.ContainsKey(key) && !String.IsNullOrEmpty(key))
this._prms.Remove(key);
}
 
public object TryGetValue(string key)
{
object obj;
_prms.TryGetValue(key, out obj);
return obj;
}
 
public void Clear()
{
this._prms.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.