ARTICLE

How to start or stop a Windows Service using VB.NET

Posted by Mahesh Chand Articles | VB.NET Windows Service August 04, 2010
How to start or stop a Windows Service using VB.NET
 
Reader Level:

We can start, stop, pause, continue and refresh a service using Start, Stop, Pause, Continue, and Refresh methods.  Close method disconnects this ServiceController instance from the service and frees all the resources that the instance allocated.

The following code snippet checks if a service is stopped, start it; otherwise stop it.

Let's say, you have a service named "MyServiceName". First you create a ServiceController object and then call its Start or Stop methods to start and stop a windows service.

    Private Sub StartStop()

        Dim service As ServiceController = New ServiceController("MyServiceName")

        If ((service.Status.Equals(ServiceControllerStatus.Stopped)) Or

            (service.Status.Equals(ServiceControllerStatus.StopPending))) Then

            service.Start()

        Else

            service.Stop()

        End If

    End Sub

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor