[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Win Service Start+Stop from OTHER code

I've created a C# WinService.
How do I Start and Stop the win service from another application written in VB.NET?
0
volking
Asked:
volking
1 Solution
 
joechinaCommented:
A simple way is to run command

NET START "xxxxxyourservicedescriptionxxxxx"
NET STOP "xxxxxyourservicedescriptionxxxxx"

0
 
PaulHewsCommented:
First of all, add a Service Controller component to your project.  In the service controller properties, set the name of the service.  (Not the display name, but the short name that you would use with NetSvc command line.)

Now it's as easy as:

Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
    If ServiceController1.Status = ServiceControllerStatus.Stopped Then
        ServiceController1.Start()
    End If
End Sub



Private Sub cmdStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStop.Click
    If ServiceController1.Status = ServiceControllerStatus.Running Then
        ServiceController1.Stop()
    End If
End Sub


To show the status:

Private Sub DisplayStatus()

    ServiceController1.Refresh()

    Select Case ServiceController1.Status

        Case ServiceControllerStatus.ContinuePending
            Label1.Text = "ContinuePending"
        Case ServiceControllerStatus.Paused
            Label1.Text = "Paused"
        Case ServiceControllerStatus.PausePending
            Label1.Text = "PausePending"
        Case ServiceControllerStatus.Running
            Label1.Text = "Running"
        Case ServiceControllerStatus.StartPending
            Label1.Text = "StartPending"
        Case ServiceControllerStatus.Stopped
            Label1.Text = "Stopped"
        Case ServiceControllerStatus.StopPending
            Label1.Text = "StopPending"

    End Select
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    DisplayStatus()
    Application.DoEvents()

End Sub
0
 
volkingAuthor Commented:
WOW ... that was easy ... THANKS PAUL!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now