title: C# Windows Service - Started and then Stopped Automatically
body: <p>I am creating this windows service by following the instructions at <a href="http://msdn.microsoft.com/en-us/library/zt39148a.aspx" rel="nofollow noreferrer">MSDN Walkthrough: Creating a Windows Service</a> and after successful installation, I go to Services.msc to Start the Windows service and before it finishes starting up I get the following message:</p>
  
  <blockquote>
    <p>The EIWindowsService service on Local Computer started and then stopped.  Some services stop automatically if they are not in use by other services or programs.</p>
  </blockquote>
  
  <p>I know the Windows Service starts ok because there is an entry to the log file stating that the service started.  I did some research before posting on here and the answer from <a href="https://stackoverflow.com/questions/6356040/some-services-stop-automatically-if-they-are-not-in-use-by-other-services/6360148#6360148">Some Services Stop Automatically</a> states that the problem could either be that the OnStart method is throwing an error, or that the OnStart is not kicking off a thread.  So I modified my code so that the only thing within the OnStart is the starting of two timers and the log entry therefore needing no exception handling.  I also added a thread to "jump" to another method.  </p>
  
  <p>I tried the windows service again and I know that it "moved" to the new method that the thread pointed to because I had a log entry in there that threw aFormatException error due to some conversion I was doing.  I commented out the conversion and the windows service still just began to start up and then stopped automatically.</p>
  
  <p>Further research indicated to me that I might need a loop to keep the processing within the method, so I took information from <a href="https://stackoverflow.com/questions/7112093/c-windows-service-the-service-on-local-computer-started-and-then-stopped/7112149#7112149">C - Windows Service the service on</a> and set up an infinite while loop.  I also found that there might be Garbage Collection going on and established a KeepAlive statement for the timers as suggested in Examples section of <a href="http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx" rel="nofollow noreferrer">MSDN Timer Class</a>.  Still the same issues.  </p>
  
  <p>At this point I feel I've exhaused all the research I can do so it would be appropriate to post my question here.  All my code is below and I will note that before I performed any change I uninstalled the Windows Service, removed the Setup Project, and deleted the installers from the C# code.  I then made changes and started back over with the instructions in the Walkthrough starting at the point where it instructs how to setup the installers.  I did this each time because I found that if I made changes and did not uninstall the Windows Service, remove the Setup Project, and delete the installers, then my changes would not take effect on the currently installed windows service.</p>
  
  <p>Any assistance you can give would be most appreciated.  I will be here for another 15min and then I will check this first thing tomorrow.</p>
  
  <p><strong>SERVICE1.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using System.Timers;
  
  namespace EIWindowsService
  {
      public partial class Service1 : ServiceBase
      {
          Logs.ErrorLog logFile = new Logs.ErrorLog();
          private System.Threading.Thread onStartThread;
  
          public Service1()
          {
              InitializeComponent();            
          }
  
          protected override void OnStart(string[] args)
          {
              try
              {
                  iTimer.Start();
                  iTimer.Elapsed += new ElapsedEventHandler(iTimer_Elapsed);
                  pTimer.Start();
                  pTimer.Elapsed += new ElapsedEventHandler(pTimer_Elapsed);                
                  onStartThread = new System.Threading.Thread(TimerValue);
                  onStartThread.Start();
                  logFile.SendToLog("EIWindows Service started on " + GetDate());
              }
              catch (ArgumentOutOfRangeException ex)
              {
                  logFile.SendToLog("ArgumentOutOfRangeException", "EIWindowsService\\Service1.cs", "OnStart()", ex);
              } //end of ArgumentOutOfRangeException CATCH statement
          }
  
          protected override void OnStop()
          {
              iTimer.Stop();
              pTimer.Stop();
              logFile.SendToLog("EIWindowsService\\Service1.cs", "OnStop()", "EIWindows Service stopped on " + GetDate());
  
          }
  
          private void TimerValue()
          {
              try
              {
                     /*commented out because it was throwing an exception error*/
                     //double iTimerValue = Convert.ToDouble(iTimer.ToString());
                     //double pTimerValue = Convert.ToDouble(pTimer.ToString());
                  while (1 &gt; 0)
                  {
                         //if (iTimerValue % 1800000 == 0)  //if the timer hits the 30min mark
                         //{
                         //    logFile.SendToLog("Current iTimer Value = " + iTimerValue.ToString());
                         //}
                         //if (pTimerValue % 1800000 == 0)  //if the timer hits the 30min mark
                         //{
                         //    logFile.SendToLog("Current pTimer Value = " + pTimerValue.ToString());
                         //}
                      GC.KeepAlive(iTimer);
                      GC.KeepAlive(pTimer);
                  }
                     //TimerValue();
              }
              catch (OverflowException ex)
              {
                  logFile.SendToLog("OverflowException", "EIWindowsService\\Service1.cs", "TimerValue()", ex);
              } //end of OverflowException CATCH statement
              catch (ArgumentException ex)
              {
                  logFile.SendToLog("ArgumentException", "EIWindowsService\\Service1.cs", "TimerValue()", ex);
              } //end of ArgumentException CATCH statement
              catch (FormatException ex)
              {
                  logFile.SendToLog("FormatException", "EIWindowsService\\Service1.cs", "TimerValue()", ex);
              } //end of FormatException CATCH statement
          }
  
          private string GetDate()
          {
              string current = "No Date Recorded";
              try
              {
                  current = DateTime.Now.ToString("F");
              }
              catch (FormatException ex)
              {
                  logFile.SendToLog("FormatException", "EIWindowsService\\Service1.cs", "GetDate()", ex);
              } //end of FormatException CATCH statement
  
              return current;
          } //end of method GetDate
  
          private void iTimer_Elapsed(object source, ElapsedEventArgs e)
          {
              try
              {
                  iTimer.Stop();
                  ImportI();
                  iTimer.Start();
              }
              catch (ArgumentOutOfRangeException ex)
              {
                  logFile.SendToLog("ArgumentOutOfRangeException", "EIWindowsService\\Service1.cs", "iTimer_Elapsed()", ex);
              } //end of ArgumentOutOfRangeException CATCH statement
          } //end of method iTimer_Elapsed
  
          private void pTimer_Elapsed(object source, ElapsedEventArgs e)
          {
              try
              {
                  pTimer.Stop();
                  ImportP();
                  pTimer.Start();
              }
              catch (ArgumentOutOfRangeException ex)
              {
                  logFile.SendToLog("ArgumentOutOfRangeException", "EIWindowsService\\Service1.cs", "pTimer_Elapsed()", ex);
              } //end of ArgumentOutOfRangeException CATCH statement
          } //end of method pTimer_Elapsed
  
          private void ImportI()
          {
              //does some action but commented out because it never gets here and is not relavant to this question.
          } //end of method ImportI
  
          private void ImportP()
          {
              //does some action but commented out because it never gets here and is not relavant to this question.
          } //end of method ImportP
      }
  }
  </code></pre>
  
  <p><strong>SERVICE1.DESIGNER.CS</strong>  (the relavant stuff)</p>
  
  <pre><code>private void InitializeComponent()
          {
              this.components = new System.ComponentModel.Container();
              this.pTimer = new System.Timers.Timer(10800000);  //3hrs
              this.iTimer = new System.Timers.Timer(3600000);  //1hr
              // 
              // pTimer
              // 
              this.pTimer.Enabled = true;
              // 
              // iTimer
              // 
              this.iTimer.Enabled = true;
              // 
              // Service1
              // 
              this.ServiceName = "EIWindowsService";
  
          }
  
          #endregion
  
          private System.Timers.Timer pTimer;
          private System.Timers.Timer iTimer;
  </code></pre>