Reputation: 3859
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
Upvotes: 1
Views: 3180
Reputation: 32651
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()
{
}
Upvotes: 1