title: .NET windows service aborted during system shutdown
body: <p>My windows service is a data server with substantial cache. During service OnStop I save the cache so that no data is lost. Saving cache may take several minutes so to prevent windows service manager from timeout I use SetServiceStatus Win32 callback:</p>
  
  <pre><code>this.serviceStatus.currentState = (int)State.SERVICE_STOP_PENDING;
  this.serviceStatus.checkPoint = 1;
  this.serviceStatus.waitHint = 60000;
  SetServiceStatus(Process.GetCurrentProcess().Handle, ref this.serviceStatus);
  </code></pre>
  
  <p>That works fine. </p>
  
  <p>I have also set CanShutdown to true and added OnShutdown so that service would be system shutdown proof. Here I effectively do the same thing as in OnStop:</p>
  
  <pre><code>protected override void OnShutdown()
  {
      this.OnStop(); 
      base.OnShutdown();
  }
  </code></pre>
  
  <p>That does not work too good. When system shuts down, when cache is being saved I get "The device is not ready". This suggests that Windows aborts service before it is done stopping / shutting down. Preventing that with SetServiceStatus apparently does not work.</p>
  
  <p>How do I get more time (delay reboot) to get saving done?</p>
  
  <p>Any suggestions welcome.</p>