Managing Windows Services with Visual Studio 2005

You may have a large number of services running on your computer with your machine using the Windows operating system. These can be managed from the user interface of your computer. These can also be managed using the Net 2.0 framework class ServiceController. This tutorial shows you how you may manage programmatically the Windows services by means of an example after describing the process of working with the Services window in the control panel. In this process several other properties and methods of this important class are also explored.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
January 03, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Accessing Windows Services using the Control Panel

Windows services running on your machine may be accessed from Control Panel-> Administrative Tools-->Services. This brings up the services window in your computer as shown. The next picture shows the services that are installed on this machine (Windows XP Professional, Media Center Edition). As you can see, they are all in different states (some stopped, some running, and so on).

The highlighted service has the service name OracleServiceXE. This service starts up automatically when the Window program starts up. It can be paused and restarted; if it has stopped, it can be started from the panel by clicking the links shown in the above window.

Properties of the Service

You may access the property window by right clicking the name of the service, which brings up the properties window as shown in the next picture. Currently the start up type has been set automatically when the Windows program is executed. This window also shows the path to the executable and the file name of the executable. Additionally you may start, pause and stop from this window. The service name is the name recognized by the system and may be different from the display name.

LogOn tab of the Services

When you click on the LogOn tab you bring up the LogOn related items of the service as shown in the next window. The picture shows the default option for LogOn as Local System Account.

This may be changed to a user account by choosing the other option, which gives you access to the users by using the Browse... button.

The Hardware profile is the hardware profile that you may set up by using Control Panel--> System and choosing the Hardware tab in the System Properties window as shown.

Handling Loss of Service

In case the service fails for any reason, this tab helps you set options as to what actions may be taken. You may set the first failure option to be Restart the Service; and you can specify after how many minutes to restart as this item becomes active. For the second failure you may want to run a specified program by browsing to its location.

Dependencies

Here you may be able to see the services that are required for this service as well as services which depend on this service.

While OracleXEClrAgent does not depend on other services, some of them do depend on other services. For example the Windows Management Instrumentation service depends on the RPC service; and the Security Center and Windows Firewall/ICS both depend on WMI.

Programmatic access using the NET framework

The System.ServiceProcess namespace has classes that help you to work with  Windows Services. These long running programs without a user interface can be programmatically implemented, installed, and managed by using these classes. In this tutorial a couple of properties and methods of the ServiceController class will be described. The next picture shows the details of the ServiceController in the Object Browser.

Finding all services running on the machine

The object browser will show you that the getServices() method would find all the services on your machine. Obviously this will create an array consisting of all the services. Services are known by their name. The following code will therefore show all the services. You may use the WriteLine() method of Debug to write to an output window, or use a list box and populate it with the names of services as shown in the next listing. In this listing "Hodentek" is the machine name.

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
Dim mysvcCntrl As ServiceController
Dim myWinSvcs As Array
myWinSvcs = ServiceController.GetServices("Hodentek")
For Each mysvcCntrl In myWinSvcs
Debug.WriteLine(mysvcCntrl.ServiceName)
ListBox1.Items.Add(mysvcCntrl.ServiceName)
Next
End Sub

On similar lines you can access other properties also. The next picture shows the list box populated by the services on the "Hodentek" computer.

Starting, Pausing and Stopping a Service

Service Status

These actions can be managed by using the methods of the Service Controller class. Some services have only starting and stopping while others may have the option of being paused. You may verify if the service has stopped or started by looking at its status property. This has a number of values given by their "enums" as shown in the next two pictures.

 

 

Stopping and Starting

The code shown in the next paragraph shows how you may stop and start the service whose name is OracleXEClrAgent programmatically.

Private Sub Button3_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button3.Click
Dim mysvcCntrl As New ServiceProcess.ServiceController
mysvcCntrl.ServiceName = "OracleXEClrAgent"
MessageBox.Show(mysvcCntrl.Status)
Try
If mysvcCntrl.Status = 4 Then
mysvcCntrl.Stop()
'does not refresh in the services panel
mysvcCntrl.Refresh()
MessageBox.Show(mysvcCntrl.Status)
End If
Catch ex As Exception
MessageBox.Show(ex.InnerException.ToString)
End Try
End Sub

In the above code the program will stop the service if it is running. If it is already stopped it may throw up an exception. When a value of 1 is used instead of 4 for status in the above code, you will get the following message about the exception.

The Service Controller's other properties can also be used by using the same code. You may look up the status codes, or you can alternatively use the syntax shown in the next paragraph.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim mysvcCntrl As New ServiceProcess.ServiceController
mysvcCntrl.ServiceName = "MSSQL$MYSORIAN"
With mysvcCntrl
MessageBox.Show(.DisplayName)
MessageBox.Show(.MachineName)
MessageBox.Show(.ServiceHandle.ToString)
End With
MessageBox.Show(mysvcCntrl.status)
If (mysvcCntrl.Status = ServiceControllerStatus.Stopped) _
Then
MessageBox.Show("Starting the SQL Server")
Try
mysvcCntrl.Start()
mysvcCntrl. _
WaitForStatus(ServiceControllerStatus.Running)
MessageBox.Show(mysvcCntrl.Status)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub

After stopping or starting the service, the Service Control Manager's screen in the Control Panel does not get refreshed. To see if the change has taken place you need to refresh the view. Also to get the correct service name you should look up the property of the service in the services window, or use the code shown earlier which lists the name of the services.

Also make sure that the form on which all these various buttons are placed have the following "imports" statements.

Imports System.ServiceProcess.ServiceController
Imports System.ServiceProcess

Summary

This tutorial described how to manage the Windows service using the Control Panel in the Windows machine as well as programmatically using the appropriate namespace class in the Visual Studio 2005 IDE.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials