Service is running?

  • Thread starter Thread starter Kevin L
  • Start date Start date
Kevin,
I normally use ServiceController.Status:

http://msdn.microsoft.com/library/d...eProcessServiceControllerClassStatusTopic.asp


' Toggle the Telnet service -
' If it is started (running, paused, etc), stop the service.
' If it is stopped, start the service.
Dim sc As New ServiceController("Telnet")
Console.WriteLine("The Telnet service status is currently set to
{0}", sc.Status)

If sc.Status = ServiceControllerStatus.Stopped _
Or sc.Status = ServiceControllerStatus.StopPending Then
' Start the service if the current status is stopped.
Console.WriteLine("Starting the Telnet service...")
sc.Start()
Else
' Stop the service if its status is not set to "Stopped".
Console.WriteLine("Stopping the Telnet service...")
sc.Stop()
End If

' Refresh and display the current service status.
sc.Refresh()
Console.WriteLine("The Telnet service status is now set to {0}.",
sc.Status)

If you are changing the status, ServiceController.WaitForStatus can be
useful:

http://msdn.microsoft.com/library/d...ServiceControllerClassWaitForStatusTopic1.asp

For example, to "restart" a service I use something like:

Dim sc As New ServiceController("Telnet")
sc.Stop()
sc.WaitForStatus(ServiceControllerStatus.Stopped)
sc.Start()

Hope this helps
Jay
 
How do I check to see if a particular service is running?

You might want to check out the System.ServiceProcess.ServiceController
class...

Looks like you should be able to do something like...

Imports System.ServiceProcess

....

Dim controller As New ServiceController ("YourSeviceName")
Console.WriteLine (controller.Status)

HTH
 
Thanks Jay. Exactly what I need.



Jay B. Harlow said:
Kevin,
I normally use ServiceController.Status:

http://msdn.microsoft.com/library/d...eProcessServiceControllerClassStatusTopic.asp


' Toggle the Telnet service -
' If it is started (running, paused, etc), stop the service.
' If it is stopped, start the service.
Dim sc As New ServiceController("Telnet")
Console.WriteLine("The Telnet service status is currently set to
{0}", sc.Status)

If sc.Status = ServiceControllerStatus.Stopped _
Or sc.Status = ServiceControllerStatus.StopPending Then
' Start the service if the current status is stopped.
Console.WriteLine("Starting the Telnet service...")
sc.Start()
Else
' Stop the service if its status is not set to "Stopped".
Console.WriteLine("Stopping the Telnet service...")
sc.Stop()
End If

' Refresh and display the current service status.
sc.Refresh()
Console.WriteLine("The Telnet service status is now set to {0}.",
sc.Status)

If you are changing the status, ServiceController.WaitForStatus can be
useful:

http://msdn.microsoft.com/library/d...ServiceControllerClassWaitForStatusTopic1.asp

For example, to "restart" a service I use something like:

Dim sc As New ServiceController("Telnet")
sc.Stop()
sc.WaitForStatus(ServiceControllerStatus.Stopped)
sc.Start()

Hope this helps
Jay
 

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

Back
Top