Software Development

Control Windows services with VB.NET's ServiceController component


Whenever you need to start, stop, pause, or pass parameters to a Windows service, Visual Studio .NET's ServiceController component makes it easy.

In order to use ServiceController, you first need to add the component to your Windows application, expand the Toolbox, and choose ServiceController. Once you add it to your form, it will appear on the bottom of the window. Click the object (which is ServiceController1 by default) and update the component's properties.

In general, the ServiceController component is very useful for sending the commands to a Windows service on a particular machine. So, for my example, I will try to start and stop a service on the local machine called WebClient. I will set the ServiceName property of the ServiceController1 to WebClient.

To stop the WebClient service on the local machine, add the following code:

ServiceController1.Stop()

To start the WebClient service on the local machine, add the following code:

ServiceController1.Start()

To verify that the code is behaving as expected, open Services under the Microsoft Management Console either by going to Control Panel | Administrative Tools | Services or Start | Run and typing Services.msc. Locate the service you are trying to start and stop (in our example, it's WebClient). After running the code above and refreshing the Services view, you should confirm that the service has responded to the command.

The ServiceController component also allows you to access services on different machines by specifying the MachineName property. Visit MSDN for details about using the ServiceController.MachineName Property.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

7 comments
darryl.de.jesus
darryl.de.jesus

Question:: Is it possible to set the cache usage of a user-created windows service? I have created a windows service which runs at the background, the ideal usage for this is 500 MB, but it actually runs consuming 3GB of cache memory. Any idea how to solve this problem, i'm using VB.Net using Visual Studio 2005. Please email me at darryl.de.jesus@gmail.com Your help is very much appreciated. Warm Regards, Darryl Than

osman
osman

My problem is security. I have an application. It must be managed a windows service. My ServiceManager application use a loggin system. But if this user is not administrator on this computer. I don't reach this service with ServiceContoller. Can i use this component to reach remote machine without admin permission.

leigh
leigh

ok, if I can run this from an aspx page it would be perfect for me. I have a need to auto email from a server using cdonts but have had problems with hackers/spammers using this service if it is turned on. If I can default the mailserver service to off and turn it on and off as required it would be great. Is this as easy as this implies?

Justin James
Justin James

What about security? For example, if a non-administrative user runs this code, what exactly happens? Does it throw an exception, pop up a UAC prompt (under Vista), or what? Very curious. J.Ja

Justin James
Justin James

For safety and security, your ASP.Net pages should be running as the anonymous user, not someone trusted enough to control services. J.Ja

ajay_whiz
ajay_whiz

It will throw an InvalidOperationException exception

Editor's Picks