Problem with ServiceController

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • northstar7
    New Member
    • May 2009
    • 2

    Problem with ServiceController

    I'm using a ServiceControll er to try to stop and start a service. For development, I've been using the Apache service to experiment with.

    I have a simple windows form in vb.net with a start and a stop button, along with a servicecontroll er added to the form.

    The start button calls ServiceControll er1.Start and the stop button ServiceControll er1.Stop. Each works individually.

    However, if I start the service with one button and then wait and try to stop it, it doesn't do anything. Or, if I stop the service with the stop button and then try to start, again nothing happens.

    If I close the application and then reopen it, it will either stop or start the service. But not stop AND start.

    Do you have any idea why this would be?


    Here's the code:

    Code:
        Private Sub btnStartService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartService.Click
            If (ServiceController1.Status.ToString <> "Running") Then
                ServiceController1.Start()
            End If
        End Sub
    
        Private Sub btnStopService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopService.Click
            If ServiceController1.Status.ToString <> "Stopped" Then
                ServiceController1.Stop()
            End If
        End Sub
    Last edited by tlhintoq; May 30 '09, 02:20 AM. Reason: [CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Maybe the .Status property is not reporting correctly??

    Try commenting the "if" conditions just for testing. That way if you hit 'Stop' it tries to stop no matter the Status. If that works, you know the problem is related to the Status condition or the way you are testing it.

    Comment

    • northstar7
      New Member
      • May 2009
      • 2

      #3
      Hi, thanks for the reply. You're right. It needed a ServiceControll er.refresh.

      That fixed it.

      Comment

      Working...