title: Getting a Topshelf service to run once on boot, then stop
body: <p>I have written a TopShelf service / console app, which appears to be running as intended, except I would like it to run once on boot, then disable itself until the next boot/reboot.</p>
  
  <p>I was hoping this would work:</p>
  
  <pre><code>class MyServiceClass
  {
      public void Start()
      {
          // do the things that need doing
  
          this.Stop();
      }
  
      public void Stop()
      {
      }
  </code></pre>
  
  <p>But that doesn't work, presumably because the this.Stop() command is there for cleanup, not for causing the service to stop.</p>
  
  <p>My Program.cs looks like this:</p>
  
  <pre><code>// Uses Topshelf: http://topshelf-project.com/
  // Guided by: http://www.ordina.nl/nl-nl/blogs/2013/maart/building-windows-services-with-c-and-topshelf/
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  
  using Topshelf;
  
  namespace MyNamespace
  {
      class Program
      {
          static void Main(string[] args)
          {
              HostFactory.Run(hostConfigurator =&gt;
              {
                  hostConfigurator.Service&lt;MyServiceClass&gt;(serviceConfigurator =&gt;
                  {
                      serviceConfigurator.ConstructUsing(() =&gt; new MyServiceClass());
                      serviceConfigurator.WhenStarted(myServiceClass =&gt; myServiceClass.Start());
                      serviceConfigurator.WhenStopped(myServiceClass =&gt; myServiceClass.Stop());
                  });
  
                  hostConfigurator.RunAsLocalSystem();
  
                  hostConfigurator.SetDisplayName("MyService");
                  hostConfigurator.SetDescription("Does stuff.");
                  hostConfigurator.SetServiceName("MyService");
                  hostConfigurator.StartAutomatically();
                  hostConfigurator.EnableShutdown();
              });
          }
      };
  }
  </code></pre>
  
  <p>How do I go about stopping the service at the end of execution?</p>
  
  <p>UPDATE: Based on Damien's input, I now have:</p>
  
  <pre><code>public class MyServiceClass
  {
      private readonly Task task;
      private HostControl hostControl;
  
      public MyServiceClass()
      {
          task = new Task(DoWork);
      }
  
      private void DoWork()
      {
          Console.WriteLine("Listen very carefully, I shall say this only once");
          hostControl.Stop();
      }
  
      public void Start(HostControl hostControl)
      {
          // so we can stop the service at the end of the check
          this.hostControl = hostControl;
  
          // start the DoWork thread
          task.Start();
      }
  
      public void Stop()
      {
  
      }
  
  };
  </code></pre>
  
  <p>and an updated Program.cs</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
  
          HostFactory.Run(hostConfigurator =&gt;
          {
              hostConfigurator.Service&lt;MyServiceClass&gt;((serviceConfigurator =&gt;
              {
                  serviceConfigurator.ConstructUsing(() =&gt; new MyServiceClass());
                  serviceConfigurator.WhenStarted((myServiceClass, hostControl) =&gt; myServiceClass.Start(hostControl));
                  serviceConfigurator.WhenStopped(myServiceClass =&gt; myServiceClass.Stop());
              });    /* compiler thinks there's a ")" missing from this line */
  
              hostConfigurator.RunAsLocalSystem();
  
              hostConfigurator.SetDisplayName("MyService");
              hostConfigurator.SetDescription("Does stuff.");
              hostConfigurator.SetServiceName("MyService");
              hostConfigurator.StartAutomatically();
              hostConfigurator.EnableShutdown();
          });
  
      }
  };
  </code></pre>
  
  <p>However, this will not compile. My compiler suggests that a ")" is missing on (or around) my comment (shown in the code above), but any addition of a close parenthesis just adds to the error list.</p>
  
  <p>I feel like I'm close... any ideas?</p>