Skip to content

Instantly share code, notes, and snippets.

@happygrizzly
Last active October 5, 2015 10:26
Show Gist options
  • Save happygrizzly/f278b8091d6989f90a33 to your computer and use it in GitHub Desktop.
Save happygrizzly/f278b8091d6989f90a33 to your computer and use it in GitHub Desktop.
namespace your_project_name
{
using System.Threading;
public abstract class PollerServiceBase : System.ServiceProcess.ServiceBase {
protected Thread workerThread = null;
protected AutoResetEvent finishedEvent = new AutoResetEvent(false);
public PollerServiceBase(string serviceName) {
if(string.IsNullOrEmpty(serviceName)) {
throw new System.ArgumentNullException(paramName: "serviceName", message: "service name is required!");
}
this.ServiceName = serviceName;
this.CanStop = true;
this.AutoLog = true;
this.EventLog.EnableRaisingEvents = true;
}
protected override void OnStart(string[] args) {
this.finishedEvent.Reset();
this.workerThread = new Thread(this.Poll);
this.workerThread.Start();
}
protected override void OnStop() {
this.finishedEvent.Set();
if(!this.workerThread.Join(2000)) {
this.RequestAdditionalTime(5000);
}
}
protected abstract void Poll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment