Checking that a Windows Service is running and restarting if stopped

S

Simon Verona

My software relies on a third party data provider to a propretary database.

In turn the third party database relies on a Windows Service that runs on
the server to supply the data... The problem is that from time to time this
service dies without warning.

Is there any way that I can write another small service or similar (using
vb.net) that checks at regular intervals that the service is running and
restart it if it isn't???

Can I check that the service is running by searching for the service
executable in the process list? or is there an easier way? What do I need
to code to start the service?

Thanks in advance

Simon
 
J

JohnFol

This will get you started, but make sure you add a reference to
System.ServiceProcess.


Dim services() As System.ServiceProcess.ServiceController

Dim i As Integer

services = System.ServiceProcess.ServiceController.GetServices()

For i = 0 To services.Length - 1

If services(i).ServiceName = "Wibble" Then

If services(i).Status = ServiceControllerStatus.Stopped Then

services(i).Start()

End If

End If

Next
 
J

Jay B. Harlow [MVP - Outlook]

Simon,
In addition to JohnFol's example, I normally simply create the
ServiceController directly, without using the ServiceController.GetServices
method.

Dim service As New ServiceController("Wibble")


service.Refresh()
If service.Status = ServiceControllerStatus.Stopped Then
service.Start()
End If

If I had the above in a loop, I would consider using
ServiceController.WaitForStatus, instead.

service.Refresh()
service.WaitForStatus(ServiceControllerStatus.Stopped)
service.Start()

Then I would consider including the timeout (TimeSpan) parameter to
WaitForStatus so as to allow a controlled exit from WaitForStatus...

Hope this helps
Jay


| My software relies on a third party data provider to a propretary
database.
|
| In turn the third party database relies on a Windows Service that runs on
| the server to supply the data... The problem is that from time to time
this
| service dies without warning.
|
| Is there any way that I can write another small service or similar (using
| vb.net) that checks at regular intervals that the service is running and
| restart it if it isn't???
|
| Can I check that the service is running by searching for the service
| executable in the process list? or is there an easier way? What do I
need
| to code to start the service?
|
| Thanks in advance
|
| Simon
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top