title: Could not Start up Windows Service after Installation
body: <p>I followed this article to build my windows service apps. </p>
  
  <p><a href="https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer</a></p>
  
  <p>Now my project directory has <code>MyService.cs</code> and <code>ProjectInstaller.cs</code>. Both contains a <code>Designer.cs</code> and a <code>.resx</code> file. </p>
  
  <p>After building the project, I copied everything under /bin/Debug to server, and ran the <code>InstallUtil</code> command to install it. </p>
  
  <p>I could see my service in Services list, but <strong>when I click Start, it will take long time to start until it gets timed-out.</strong> </p>
  
  <p>My Program.cs file is simple as normal</p>
  
  <pre><code>public static void Main()
          {
  
  
  #if DEBUG
  
              MyService mySvc = new MyService();
              mySvc.OnDebug();
              System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
  #else
  
              ServiceBase[] ServicesToRun;
              ServicesToRun = new ServiceBase[] 
              { 
                  new MyService() 
              };
              ServiceBase.Run(ServicesToRun);
  #endif
          }
  </code></pre>
  
  <p>In <code>MyService.cs</code>:</p>
  
  <pre><code>public partial class MyService : ServiceBase
      {
          private bool stopping = false;
          private Int32 timeInterval = 0;
          private ManualResetEvent stoppedEvent;
          public static IServiceProvider svcProvider = null;
  
          public MyService()
          {
              InitializeComponent();
              stopping = false;
              stoppedEvent = new ManualResetEvent(false);
              LoadDIStartup();
          }
  
          public void OnDebug()
          {
              StartServiceWorkerMainProcess();
          }
  
          protected override void OnStart(string[] args)
          {
              stopping = false;
              StartServiceWorkerMainProcess();
          }
  
          protected override void OnStop()
          {
              stopping = true;
              StopServiceWorkerMainProcess();
              stoppedEvent.WaitOne(); //Wait for the finish of the ServiceWorkerThread
          }
  
          /// &lt;summary&gt;
          /// The function called in the start event of the the service. And when in Visual Studio Debug Mode run.
          /// &lt;/summary&gt;
          public void StartServiceWorkerMainProcess()
          {
              try
              {
                  timeInterval = AppConfig.TimeInterval;
                  // Queue the SubWorker for execution in a worker thread. 
                  ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerSub));
              }
              catch (Exception e)
              {
                  AppLogger.LogError(" Error while launching the WorkerSub thread. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");
  
              }
          }
  
          /// &lt;summary&gt;
          /// The function called in the stop event of the the service
          /// &lt;/summary&gt;
          public void StopServiceWorkerMainProcess()
          {
              AppLogger.LogInfo(" Service Stopped at " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\n");
          }
  
          private async void ServiceWorkerSub(object state)
          {
              try
              {
                  // Periodically check if the service is stopping
                  while (!stopping)
                  {
                      try
                      {
                          //Do my Stuff (FYI, Mutex is used here.)
                          AppLogger.LogInfo("DONE. About to sleep.");
                      }
                      catch (Exception e)
                      {
                          AppLogger.LogError(" Error. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");
  
                      }
                      Thread.Sleep(timeInterval);
                  }
                  // Signal the stopped event
                  stoppedEvent.Set();
              }
              catch (Exception e)
              {
                  AppLogger.LogError(" Error in ServiceWorkerSub. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");
  
              }
          }
  
          private static void LoadDIStartup()
          {
              //Dependency Injection Setup Start
              // blah blah. DI using json files. 
              //Dependency Injection Setup End
  
          }
      }
  </code></pre>
  
  <p>In Designer view, MyService.cs has a <code>serviceController1</code> and I set serviceName = <code>TestService</code>. ProjectInstaller.cs has a <code>serviceProcessInstaller1</code> and <code>serviceInstaller1</code>. The serviceName property in serviceInstaller1 is <code>TestService</code> as well. </p>
  
  <p>The Project has several properties file inside and it runs well locally. </p>
  
  <p>Any problem with my setup? </p>