I have a web service which I need to ensure will finish processing then exit when onstop() is called. Currently when onstop() is called the service immediately stops. I've been told to look at ManualResetEvent and a requeststop flag. I've looked everywhere for examples even found a few of these:
How do I safely stop a C# .NET thread running in a Windows service?
To make a choice between ManualResetEvent or Thread.Sleep()
http://www.codeproject.com/Articles/19370/Windows-Services-Made-Simple
But I've having so much trouble understanding which one I can best apply to my situation.
Code below:
System.Timers.Timer timer = new System.Timers.Timer();
private volatile bool _requestStop = false;
private static readonly string connStr = ConfigurationManager.ConnectionStrings["bedbankstandssConnectionString"].ConnectionString;
private readonly ManualResetEvent _allDoneEvt = new ManualResetEvent(true);
public InvService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
_requestStop = false;
timer.Elapsed += timer_Elapsed;
double pollingInterval = Convert.ToDouble(ConfigurationManager.AppSettings["PollingInterval"]);
timer.Interval = pollingInterval;
timer.Enabled = true;
timer.Start();
}
protected override void OnStop()
{
_requestStop = true;
timer.Dispose();
}
protected override void OnContinue()
{ }
protected override void OnPause()
{ }
private void timer_Elapsed(object sender, EventArgs e)
{
if (!_requestStop)
{
timer.Start();
InvProcessingChanges();
}
}
private void InvProcessingChanges()
{
//Processes changes to inventory
}
Is anybody experienced with windows services there who can help me? I'm quite new to windows services having just completed my first working service. This service needs to complete sending inventory updates before it actually stops.