windows service doesn't run at regular intervals
<p>I have developed windows service for checking some services running or not for every two minutes.if the services are not running then start them automatically.</p>

<p>here is my code</p>

<pre><code>public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            this.CheckServices();

            this.ScheduleService();


        }

        protected override void OnStop()
        {
            this.Schedular.Dispose();
        }




        public void CheckServices()
        {

            log4net.Config.BasicConfigurator.Configure();
            log4net.ILog log = log4net.LogManager.GetLogger(typeof(Service1));
            try
            {

                string[] arr1 = new string[] { "CaseWorksCachingService", "Te.Service" };
                for (int i = 0; i &lt; arr1.Length; i++)
                {
                    ServiceController service = new ServiceController(arr1[i]);

                    service.Refresh();

                    if (service.Status == ServiceControllerStatus.Stopped)
                    {
                        service.Start();

                    }
                }


            }
            catch (Exception ex)
            {
                log.Error("Error Message: " + ex.Message.ToString(), ex);
            }


        }

        //ScheduleService Method

        private Timer Schedular;


        public void ScheduleService()
        {
            try
            {
                Schedular = new Timer(new TimerCallback(SchedularCallback));
                string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();


                //Set the Default Time.
                DateTime scheduledTime = DateTime.MinValue;



                if (mode.ToUpper() == "INTERVAL")
                {
                    //Get the Interval in Minutes from AppSettings.
                    int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);

                    //Set the Scheduled Time by adding the Interval to Current Time.
                    scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
                    if (DateTime.Now &gt; scheduledTime)
                    {
                        //If Scheduled Time is passed set Schedule for the next Interval.
                        scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
                    }
                }

                TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);


                //Get the difference in Minutes between the Scheduled and Current Time.
                int dueTime = Convert.ToInt32(timeSpan.TotalMilliseconds);

                //Change the Timer's Due Time.
                Schedular.Change(dueTime, Timeout.Infinite);
            }
            catch (Exception ex)
            {


                //Stop the Windows Service.
                using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("MyFirstService"))
                {
                    serviceController.Stop();
                }
            }
        }

        private void SchedularCallback(object e)
        {
            //this.WriteToFile("Simple Service Log: {0}");
            this.CheckServices();
            this.ScheduleService();
        }
    }
</code></pre>

<p>and here is my app.config file</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
  &lt;appSettings&gt;
    &lt;add key ="Mode" value ="Interval"/&gt;
    &lt;!-- &lt;add key ="Mode" value ="Interval"/&gt;--&gt;
    &lt;add key ="IntervalMinutes" value ="2"/&gt;

  &lt;/appSettings&gt;

  &lt;configSections&gt;
    &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=2.0.5, Culture=neutral, PublicKeyToken=1b44e1d426115821" /&gt;
  &lt;/configSections&gt;
  &lt;!-- Log4net Logging Setup --&gt;
  &lt;log4net&gt;
    &lt;appender name="FileAppender" type="log4net.Appender.FileAppender,log4net"&gt;
      &lt;file value="C:\\mylogfile1.txt" /&gt;
      &lt;!-- the location where the log file would be created --&gt;
      &lt;appendToFile value="true" /&gt;
      &lt;lockingModel type="log4net.Appender.FileAppender+MinimalLock" /&gt;
      &lt;layout type="log4net.Layout.PatternLayout"&gt;
        &lt;conversionPattern value="%date [%thread] %level %logger - %message%newline" /&gt;
      &lt;/layout&gt;
      &lt;filter type="log4net.Filter.LevelRangeFilter"&gt;
        &lt;levelMin value="INFO" /&gt;
        &lt;levelMax value="FATAL" /&gt;
      &lt;/filter&gt;
    &lt;/appender&gt;
    &lt;root&gt;
      &lt;level value="DEBUG" /&gt;
      &lt;appender-ref ref="FileAppender" /&gt;
    &lt;/root&gt;
  &lt;/log4net&gt;
&lt;/configuration&gt;
</code></pre>

<p>Error: "The Windows Search service on local computer started and then stopped. Some services stop automatically if they are not in use by other services or programs"</p>