title: Using ManualResetEvent to check if service has finished processing
body: <p>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:</p>
  
  <p><a href="https://stackoverflow.com/questions/1764898/how-do-i-safely-stop-a-c-sharp-net-thread-running-in-a-windows-service">How do I safely stop a C# .NET thread running in a Windows service?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1675936/to-make-a-choice-between-manualresetevent-or-thread-sleep">To make a choice between ManualResetEvent or Thread.Sleep()</a></p>
  
  <p><a href="http://www.codeproject.com/Articles/19370/Windows-Services-Made-Simple" rel="nofollow noreferrer">http://www.codeproject.com/Articles/19370/Windows-Services-Made-Simple</a></p>
  
  <p>But I've having so much trouble understanding which one I can best apply to my situation.</p>
  
  <p>Code below: </p>
  
  <pre><code>        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
          }
  </code></pre>
  
  <p>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. </p>