title: Stopping a Windows Service in the event of a critical error
body: <p>I have a Windows Service which basically wraps a task:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class Service : ServiceBase
  {
      private Task task;
      private CancellationTokenSource cancelToken;
  
      public Service()
      {
          InitializeComponent();
  
          this.task = null;
          this.cancelToken = null;
      }
  
      protected override void OnStart(string[] args)
      {
          var svc = new MyServiceTask();
          this.cancelToken = new CancellationTokenSource();
          this.task = svc.RunAsync(cancelToken.Token);
          this.task.ContinueWith(t =&gt; this.OnUnhandledException(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
      }
  
      protected override void OnStop()
      {
          if (this.task != null)
          {
              this.cancelToken.Cancel();
              this.task.Wait();
          }
      }
  
      private void OnUnhandledException(Exception ex)
      {
          this.EventLog.WriteEntry(string.Format("Unhandled exception: {0}", ex), EventLogEntryType.Error);
          this.task = null;
          this.Stop();
      }
  }
  </code></pre>
  
  <p>As you can see, the service can catch unhandled exceptions. If this happens, the exception is logged and the service is stopped. This has the effect of writing two messages to the event log - one error stating there was an unhandled exception, and another stating that the service was successfully stopped. </p>
  
  <p>This may sound minor, but I'm hoping to be able to suppress the 'successfully stopped' message. I find it misleading - it suggests that the service stopping was a normal occurrence. Is there another way I can force the service to stop itself without this message being logged?</p>