-
Jul 14th, 2016, 06:40 AM
#1
Thread Starter
Lively Member
Restarting a Windows Service Programmatically
Hi all
I've recently been doing quite a lot of work with Windows Services.
My latest project contains a total of six services. Four of those services are active between 8am and 8pm, while the other two are active between 8pm and 8am. Each service is configured to send an email to myself and the rest of our development team when it starts, stops, pauses or resumes.
I've just added a seventh service which runs various housekeeping tasks on the other 6. One of those housekeeping tasks involves a graceful stop and start of each service at a pre-configured time of day. If I say so myself, what I've built is quite sweet because when it stops a particular service, it logs the fact that it's done it but doesn't trigger the same emails to be sent on service stop & start. By adding this functionality, I've ensured that the services are never running long enough to become "stale" whilst at the same time ensuring that people are not constantly being bombarded with emails.
Here's my question though: I would like it to be possible for the housekeeper service to also be restarted on a regular basis (perhaps once per week) but would prefer not to make that a task of one of the services that the housekeeper is in control of. As with the other services, I would want the housekeeper to stop and start without telling the entire world it's doing it.
I had a hunch that it wouldn't work (which it didn't) but I tried coding the service so that it would issue the stop and start command against itself - predictably this only got as far as stopping the service. From the reading I've done, it seems that it's possible to have a service automatically-recover itself in the event of a failure, which seems like it could be the best way to go but I'm struggling to get my head around how I would code the service with the ability to make itself believe (at the correct time of day) that it had failed.
Incidentally, I'm coding this in Visual Basic.NET.
TIA
Ian
-
Jul 14th, 2016, 06:54 AM
#2
Re: Restarting a Windows Service Programmatically
This might work :
Code:
Dim controller As New ServiceController("ServiceName")
controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.S topped)
controller.Start()
VB.NET MVP 2008 - Present
-
Jul 14th, 2016, 06:59 AM
#3
Thread Starter
Lively Member
Re: Restarting a Windows Service Programmatically
Will that not give me the same issue that I've got at the moment - it'll stop itself but won't start itself?
-
Jul 14th, 2016, 09:37 AM
#4
Re: Restarting a Windows Service Programmatically
Yes and No. The Service Controller is not the service, it controls the service. So it can start and stop the service. Of course, something needs to be running the Service Controller, and down the rabbit hole we go.
Personally, if I want a service to recover I'd specify it as the recovery action of the service in the services management console of Windows.
You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Jul 14th, 2016, 09:42 AM
#5
Thread Starter
Lively Member
Re: Restarting a Windows Service Programmatically
 Originally Posted by FunkyDexter
Yes and No. The Service Controller is not the service, it controls the service. So it can start and stop the service. Of course, something needs to be running the Service Controller, and down the rabbit hole we go.
Personally, if I want a service to recover I'd specify it as the recovery action of the service in the services management console of Windows.
Yeah, I'd hoped there was a way of setting the service recovery action programmatically, so that I (or anyone else who happened to republish the service) didn't need to specifically remember to change that setting when they redeployed future versions of the service.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|