<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Writing a Managed Windows Service with C#
By John Mueller

Rate This Article: Add This Article To:

Writing a Managed Windows Service with C#
( Page 1 of 4 )

Windows services are possibly one of the most interesting and useful background applications you can create. But can you create them with managed code in C#? In fact, you can! John Mueller shows you how.

Open the Services console in the Administrative Tools folder of the Control Panel and you’re apt to see a host of services. A service is a special kind of application that sits in the background and waits for something to happen. You can call a service directly if you know the required commands or a system event can prompt a service to take action. A service can perform any act that any other application can within the limits of the security you set for it. In most cases, a service is silent—it sits in the background unobserved. Some developers combine services with applications that reside in the notification area to a user interface and the background monitoring that a service does best. Theoretically, a service can interact with the Desktop, but this is unusual.

At one time, writing a service was nearly impossible. You really had to know C++ programming techniques well. However, when .NET started to take hold, some Visual C++ developers started writing managed services. In fact, I wrote an article about it at the time, “Writing a Managed Windows Service with Visual C++” (see http://www.devsource.com/c/a/Using-VS/Writing-a-Managed-Windows-Service-with-Visual-C/ for details). As time went by, Microsoft provided the same functionality to C# developers, which is the topic of this article. In addition to the relative simplicity that C# provides, the service template has also improved, as has the .NET Framework support for services. This article presents you with a view of the latest Windows service support that .NET provides.

Starting the Project

Microsoft thoughtfully provided a template for services as part of Visual Studio. Both Visual Basic and C# have this template, so you can create a service using your favorite language. The technique is about the same using either language (although there are definitely differences in the required code). The following steps help you create the required project.

Choose File | New | Project to display the New Project dialog box shown in Figure 1.

Figure 1: Select the Windows Service template to begin your project.

Select the Visual C#\Windows folder. Highlight the Windows Service template.

Type a name for the service in the Name field. The example uses TestService.

Click OK. Visual Studio creates the new service project for you.

You’ll see a blank palette where you can place components you use with your service. For example, you might add a timer to perform service-related tasks at timed intervals. As previously mentioned, a service won’t normally have a user interface and won’t interact directly with the user. Consequently, you won’t normally add controls to the designer.

After you see the project, you need to give your service a name other than Service1. Rename the service’s file to something like MyTestService.cs. Accept changes to the class names as well. Afterward, change the ServiceName property as well. The example uses My Test Service.



 
 
>>> More Microsoft Architecture Articles          >>> More By John Mueller