Win Service Start+Stop from OTHER code

Posted on 2007-07-31
Last Modified: 2013-11-07
I've created a C# WinService.
How do I Start and Stop the win service from another application written in VB.NET?
Question by:volking
    LVL 13

    Expert Comment

    A simple way is to run command

    NET START "xxxxxyourservicedescriptionxxxxx"
    NET STOP "xxxxxyourservicedescriptionxxxxx"

    LVL 38

    Accepted Solution

    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
        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
        End If
    End Sub

    To show the status:

    Private Sub DisplayStatus()


        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

    End Sub
    LVL 5

    Author Comment

    WOW ... that was easy ... THANKS PAUL!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now