O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Start and Stop a Windows Service Programmatically

Problem

You want to start or stop a Windows service that is installed on the current computer.

Solution

Use the Stop and Start methods of the ServiceController class.

Discussion

Recipe Retrieve Information About Installed Windows Services shows the properties of the ServiceController class. The ServiceController class also provides methods such as Start, Stop, Pause, and Continue.

As an example, here’s how you might start a service programmatically:

Dim TestService As New ServiceController("TestService")
TestService.Start()

The ServiceController class methods allow you to start and stop services and monitor their status, but they won’t allow you to interact in an application-specific way. In other words, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required