title: Installed Windows service will not start
body: <p>Ok, I hate being stumped but I am this time.  I was able to get the service to install just fine so that works good.  When I start the service, it stops and I am getting no details on what is wrong.  So here is my code:</p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.Data;
  using System.Data.Common;
  using System.Data.OleDb;
  using System.Data.SqlClient;
  using System.IO;
  using System.ServiceProcess;
  using System.Text.RegularExpressions;
  using System.Threading;
  
  namespace MyServiceImport
  {
  
  public partial class MyServiceData : ServiceBase
  {
      private System.Timers.Timer _timer;
      private Double ReadTime = Convert.ToDouble(ConfigurationManager.AppSettings.Get("ReadTime")) * 1000 * 60;
      private DateTime _lastRun = DateTime.Now;
      Thread _thread;
  
      public MyServiceData()
      {
          InitializeComponent();
      }
  
      // These lines run when the service starts up
      protected override void OnStart(string[] args)
      {
          _timer = new System.Timers.Timer();
          _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
          _timer.Enabled = true;
          _timer.Interval = ReadTime;
          _timer.Start();
  
          #region Write to Audit Log
          string Event = "Update Service";
          string DoneBy = "Automation User";
          string Information = "Update Service has Started.";
          string MyTimeStamp = DateTime.Now.ToString();
  
          SqlConnection conn = new SqlConnection(DBUtils.MyService_DBString);
          string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
          SqlCommand WriteLog = new SqlCommand(writeit, conn);
          conn.Open();
          WriteLog.Parameters.AddWithValue("Event", Event);
          WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
          WriteLog.Parameters.AddWithValue("Information", Information);
          WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
          WriteLog.ExecuteNonQuery();
          conn.Close();
          #endregion
  
      }
  
      // writes message into the activity log that the service has stopped.
      protected override void OnStop()
      {
          #region Write to Audit Log
          string Event = "Update Service";
          string DoneBy = "Automation User";
          string Information = "Update Service has Stopped.";
          string MyTimeStamp = DateTime.Now.ToString();
  
          SqlConnection conn = new SqlConnection(DBUtils.MyService_DBString);
          string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
          SqlCommand WriteLog = new SqlCommand(writeit, conn);
          conn.Open();
          WriteLog.Parameters.AddWithValue("Event", Event);
          WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
          WriteLog.Parameters.AddWithValue("Information", Information);
          WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
          WriteLog.ExecuteNonQuery();
          conn.Close();
          #endregion
      }
  
      void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          // set up connection string 
          string _csMyServiceDD = DBUtils.MyService_DBString;
          string DoImportNow = string.Empty, TaskDay = string.Empty, TaskStartTime = string.Empty, TaskEndTime = string.Empty;
  
          SqlConnection conn = new SqlConnection(_csMyServiceDD);
          // Get task variables from database for comparison
          string query = "SELECT TaskDay, TaskStartTime, TaskEndTime FROM MyService_TaskSchedule where TaskName = 'DoImport'";
          SqlCommand cmd1 = new SqlCommand(query, conn);
          // opens the connection to read
          conn.Open();
          SqlDataReader rdr1 = cmd1.ExecuteReader();
          while (rdr1.Read())
          {
              TaskDay = rdr1.GetValue(0).ToString();
              TaskStartTime = rdr1.GetValue(1).ToString();
              TaskEndTime = rdr1.GetValue(2).ToString();
          }
          // closing the connection
          conn.Close();
  
          // setting _lastRun variable to this moment.
          _lastRun = DateTime.Now;
          // stop the timer 
          _timer.Stop();
          try
          {
              // check if the current time is within the time range for this service task.
              if (DateTime.Now.DayOfWeek.ToString() == TaskDay &amp;&amp; DateTime.Now &gt; Convert.ToDateTime(TaskStartTime) &amp;&amp; DateTime.Now &lt; Convert.ToDateTime(TaskEndTime))
              {
                  _thread = new Thread(new ThreadStart(ImportData));
                  _thread.Name = "ImportData";
                  _thread.Start();
              }
              // decide if there is something to do
              #region Write to Audit Log
              string Event = "Update Service";
              string DoneBy = "Automation User";
              string Information = "Update Service has started importing data.";
              string MyTimeStamp = DateTime.Now.ToString();
  
              string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
              SqlCommand WriteLog = new SqlCommand(writeit, conn);
              conn.Open();
              WriteLog.Parameters.AddWithValue("Event", Event);
              WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
              WriteLog.Parameters.AddWithValue("Information", Information);
              WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
              WriteLog.ExecuteNonQuery();
              conn.Close();
              #endregion
          }
          catch (Exception ex)
          {
              // Catch error and write to audit log
              #region Write to Audit Log
              string Event = "Update Service";
              string DoneBy = "Automation User";
              string Information = "Error occurred in Import - Message: " + ex.ToString() + "";
              string MyTimeStamp = DateTime.Now.ToString();
  
              string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
              SqlCommand WriteLog = new SqlCommand(writeit, conn);
              conn.Open();
              WriteLog.Parameters.AddWithValue("Event", Event);
              WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
              WriteLog.Parameters.AddWithValue("Information", Information);
              WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
              WriteLog.ExecuteNonQuery();
              conn.Close();
              #endregion
          }
          _timer.Start();
      }
  </code></pre>