I am following this tutorial: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx to create a windows service. I have a class called TaskManager
which uses Quartz.Net to manage a bunch of jobs. It has .Go()
(which doesn't block) and .Stop()
methods. If I've understood correctly, all I need to do in my service is
private TaskManager _taskManager;
public DataPumpService()
{
InitializeComponent();
_taskManager = new TaskManager();
}
protected override void OnStart(string[] args)
{
_taskManager.Go();
}
protected override void OnStop()
{
_taskManager.Stop();
}
But then the tutorial has a section on setting the service status. It doesn't really explain what the service status is or when I would want to set it. TaskManager.Stop()
can take a few seconds to finish (internally it call IScheduler.Interrupt()
on all jobs and then IScheduler.Shutdown(true)
). So should I be setting statuses? If so then assuming I include the code in sections (1), (2) and (3) from the Setting Service Status section of the tutorial, is it correct to do the following (essentially for both methods in my first code block above):
protected override void OnStop()
{
// Update the service state to Stop Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
_taskManager.Stop();
// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}
If this is right, then is the serviceStatus.dwWaitHint = 100000;
property something I need to choose wisely or is that default value a good thing to stick with? Essentially I have no idea what this value is for...
As @HansPassant says
But if you need to handle a long running close, the documentation says concerning
and
This clarifies the footnote in the walk-through.
Based on that, I wrote my Stop code like such. Note that, I run some very long tasks and terminating them is really not a good idea hence the lengthy wait times.