3 Replies - 1907 Views - Last Post: 29 December 2012 - 12:35 PM Rate Topic: -----

#1 nucleus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-December 12

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?

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

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: cannot open service "servicename" on computer '.'.

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: cannot open service "servicename" on computer '.'.

Posted 29 December 2012 - 07:01 AM

View Postnucleus, 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

Was This Post Helpful? 0
  • +
  • -

#3 nucleus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-December 12

Re: cannot open service "servicename" on computer '.'.

Posted 29 December 2012 - 09:24 AM

Thank you very much for your help.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: cannot open service "servicename" on computer '.'.

Posted 29 December 2012 - 12:35 PM

View Postnucleus, on 29 December 2012 - 10:24 AM, said:

Thank you very much for your help.

I'd be interested to hear how you solve the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1