Last active
October 5, 2015 10:26
-
-
Save happygrizzly/f278b8091d6989f90a33 to your computer and use it in GitHub Desktop.
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
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