Imports System.ServiceProcess
Public Class Form1
Dim Spooler As New ServiceController("Spooler")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Spooler.Status.Equals(ServiceControllerStatus.Running) Then Label1.Text = "Running" : PictureBox1.BackColor = Color.Green : Button1.Enabled = False : Button2.Enabled = True
If Spooler.Status.Equals(ServiceControllerStatus.Stopped) Then Label1.Text = "Stopped" : PictureBox1.BackColor = Color.Red : Button1.Enabled = True : Button2.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Spooler.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Spooler.Stop()
End Sub
End Class
3 Replies - 178 Views - Last Post: 29 December 2012 - 12:35 PM
#1
cannot open service "servicename" on computer '.'.
Posted 29 December 2012 - 04:27 AM
For some reason, when using the code below, when I click any of the 2 buttons to stop or start the spooler service, I get the error attached. I've googled it, but I could not find the reason why. Any ideas?
Replies To: cannot open service "servicename" on computer '.'.
#2
Re: cannot open service "servicename" on computer '.'.
Posted 29 December 2012 - 07:01 AM
nucleus, on 29 December 2012 - 05:27 AM, said:
For some reason, when using the code below, when I click any of the 2 buttons to stop or start the spooler service, I get the error attached. I've googled it, but I could not find the reason why. Any ideas?
It sounds like the service is not in the state you think it's in. According to the docs, you should be using "=" instead of ".Equals" to detect the state of the service.
Their example code for .Start...
' Check whether the Alerter service is started.
Dim sc As New ServiceController()
sc.ServiceName = "Alerter"
Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)
If sc.Status = ServiceControllerStatus.Stopped Then
' Start the service if the current status is stopped.
Console.WriteLine("Starting the Alerter service...")
Try
' Start the service, and wait until its status is "Running".
sc.Start()
sc.WaitForStatus(ServiceControllerStatus.Running)
' Display the current service status.
Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status)
Catch
Console.WriteLine("Could not start the Alerter service.")
End Try
End If
Edit: On the other hand, they use .Equals in another example:
If sc.Status.Equals(ServiceControllerStatus.Stopped) Or sc.Status.Equals(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
This post has been edited by lar3ry: 29 December 2012 - 07:07 AM
#3
Re: cannot open service "servicename" on computer '.'.
Posted 29 December 2012 - 09:24 AM
Thank you very much for your help.
#4
Re: cannot open service "servicename" on computer '.'.
Posted 29 December 2012 - 12:35 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|