2009-03-05
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 4 of 4 )
Interacting with the Service
The service you create is just like any other Windows service you work with in .NET. Open Server Explorer, locate the Services folder for your machine, locate your custom service, and drag a copy to the application form. You’ll see a new service component added to the application. The example renames this component to MyTestSvc. As with any service, you can detect its state when you start the application as shown here.
public frmMain()
{
InitializeComponent();
// Check the service status.
if (MyTestSvc.Status == ServiceControllerStatus.Running)
btnStartStop.Text = "&Stop";
else
btnStartStop.Text = "&Start";
}
In this case, the code verifies the service state and sets a button caption. This button lets you start and stop the service as needed. The code for starting and stopping the service looks like this:
private void btnStartStop_Click(
object sender, EventArgs e)
{
// Check the status.
if (btnStartStop.Text == "&Start")
{
// Change the service status.
MyTestSvc.Start();
// Set the button caption.
btnStartStop.Text = "&Stop";
}
else
{
MyTestSvc.Stop();
btnStartStop.Text = "&Start";
}
}
As you can see, the commands are simple. The service also provides standard commands for pausing and continuing the service. The custom command requires that you provide a command number as shown here.
private void btnCustomCommand_Click(object sender, EventArgs e)
{
// Send a custom command to the service.
MyTestSvc.ExecuteCommand(130);
}
Using an enumeration can make services with a lengthy list of commands easier to work with. In this case, the service only allows one command, so using the number works fine. Whenever the example application issues a command, you see an event log entry. Open the Event Viewer and you’ll see a pair of entries like the ones shown in Figure 4.
Figure 4: Each use of the custom command creates two event log entries.
The first event log entry is from the base class. It simply tells you that the service executed a custom command successfully. The second event log entry is the one written by the custom command handler. As you can see, it contains all of the information you might expect from the source code provided in the article.
Bottom Line
Using managed code makes writing Windows services considerably easier than using the native C++ code you might have relied on in the past. You can easily perform any non-graphical task that you normally perform with .NET. Services have to be ultra-reliable because they execute in the background with little in the way of supervision. Using .NET actually helps improve reliability because you no longer have to worry about issues such as memory leaks causing problems. Adding managed services to your toolbox can pay big dividends in application development—just consider the number of services that an average Windows machine already has in place.
BIO
John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 82 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads down programming. His current project is LINQ for Dummies (available now), which you can order at http://www.amazon.com/exec/obidos/ASIN/0470277947/datacservip0f-20/. His technical editing skills have helped over 60 authors refine the content of their manuscripts. You can reach John on the Internet at JMueller@mwt.net and his Web site at: http://www.mwt.net/~jmueller/.
![]() |
|


