1

I wonder how i can force my window service to restart or stop if it's running for already about 30 mins.

it's like:

if(service.runs == 30 mins){
     service.stop() 
       or
     service.restart()
}

by the way, I am using C# on this. And I am using a Thread here. This is how my OnStart looks like:

Thread myThread;

protected override void OnStart(string[] args){
     myThread = new Thread(new ThreadStart(this.myThreadFunction));

     myThreadFunction.Start();
}

Please help. Thanks

SyntaxError
  • 3,347
  • 5
  • 26
  • 31
  • 2
    Do you want to do this from within the process you're trying to stop, or do you want to stop an unrelated service? – Gabe Aug 12 '13 at 03:51
  • yes, i want to do this from within the process. – SyntaxError Aug 12 '13 at 04:05
  • Might be an answer http://stackoverflow.com/questions/3431771/windows-service-startup-time. – AgentFire Aug 12 '13 at 04:05
  • If you are to close the process from inside, then just place a timer and check current time with the time when the process was run, if the difference hits 30 minutes, exit. – AgentFire Aug 12 '13 at 04:06
  • 1
    I can see how your service might be written in C#, but I don't understand what ASP.Net has to do with this. Can you explain where that part comes in? – Gabe Aug 12 '13 at 04:13
  • forgive me Gabe, ASP.NET has nothing to do with this. its all C#. Thanks :) – SyntaxError Aug 12 '13 at 04:19
  • Google "watchdog timer." – 3Dave Aug 12 '13 at 04:23
  • Why on Earth would you want to recycle a service on such a schedule? It sounds like you want a scheduled task rather than a service. Are you sure you're asking the right question? – Jim Mischel Aug 12 '13 at 21:33

1 Answers1

1

You need to run timer on service start. And set its elapsed event to 30 mins. If it is elapsed then you can apply your above check of stopping it. You also need to Reset your timer when ever the service is stopped/restarted.

     //somewhere in your class 
     System.Timer.Timer tmr  = new System.Timers.Timer();

     //on construct or start event         
     tmr.Interval  = 1800000; //30 minutes = 60*1000*30
     tmr.Elapsed -= new ElapsedEventHandler(OnTimedEvent);
     tmr.Elapsed += new ElapsedEventHandler(OnTimedEvent);
     tmr.Start();


     private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
            tmr.Stop();
          ServiceController service = new ServiceController(yourserviceName);
          service.Stop();
         // service.Start() uncomment this line if your want to restart
     }

      protected override void OnStop()
    { 

    }
Ehsan
  • 28,801
  • 6
  • 51
  • 61
  • Thanks. I will try... ^_^ – SyntaxError Aug 12 '13 at 04:20
  • Note this will just stop the service, not restart it, but [it is not hard to tweak the code](http://stackoverflow.com/questions/220382/how-can-a-windows-service-programmatically-restart-itself) to make it restart instead of stop. – Scott Chamberlain Aug 12 '13 at 05:26
  • @ScottChamberlain you are right, actually he mentioned restart/stop so i gave one option. He can easily change that to restart as well – Ehsan Aug 12 '13 at 05:49
  • Hi Ehsan Ullah, would you mind if I request to edit your answer exactly to stop if service runs in 30 mins? I tried using this but it doesnt work for me. And by the way, I am using a Thread here..I will edit my post as well. Thanks – SyntaxError Aug 13 '13 at 02:56