title: how to make service act dynamically based on service running condition
body: <p>hi friends i was trying to make my service act dynamically... i have set time for my service about for 2 min ,if suppose it was doin huge amount of work means it will exceeds that 2 min time limit then we need to check the service condition if work is pending means we need to run that instance until upto finish</p>
  
  <p>so that i have tried this below code on googling ... i m having method were i need to cooperate in below service, can any one help me </p>
  
  <pre><code> public static void StartService(string serviceName, int timeoutMilliseconds)
     {
    ServiceController service = new ServiceController(serviceName);
    try
     {
     TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
  
      service.Start();
      service.WaitForStatus(ServiceControllerStatus.Running, timeout);
     }
     catch
    {
    // ...
    }
   }
  </code></pre>
  
  <p>as of now i m doing this below logic </p>
  
  <pre><code>  protected override void OnStart(string[] args)
      {
          // my service name 
          Workjob("FTSCSVGenerator");
          // ad 1: handle Elapsed event and CsvGenFromDatabase is method which i have to executed
          timerjob.Elapsed += new ElapsedEventHandler(CsvGenFromDatabase);
         // ad 2: set interval to 1 minute (= 60,000 milliseconds)
        timerjob.Interval = Convert.ToDouble(DueTime);
        //  ////ad 3: enabling the timer
        timerjob.Enabled = true;
        eventLog1.WriteEntry("my service started");
      }
  
      protected override void OnStop()
      {           
          eventLog1.WriteEntry("my service stopped");
      }
  
  
      private void Workjob(string servicename )
      {
          ServiceController servicecsv = new ServiceController(servicename);
  
  
           if ((servicecsv.Status.Equals(ServiceControllerStatus.Stopped)) || (servicecsv.Status.Equals(ServiceControllerStatus.StopPending)))
           {
               // Start the service if the current status is stopped.                 
               servicecsv.Start( );               
           }
           else
           {
               // Stop the service if its status is not set to "Stopped".                 
               servicecsv.Stop();
           }  
  
  
      }
  </code></pre>