Writing a Windows service

Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!

With .NET, developing a Windows Service is a piece of cake. VS.NET has a project template for creating Windows service. In this tip, I will walk you through the steps of creating a simple Windows service.

Step 1: Developing a service

From VS.NET File menu -> New -> Project, select the Windows Service project template.


The code for the WinService is pretty straight forward, there are events like:


And other events to get the most out of the Windows service.

Write the code in the respective event handlers, but remember a service will be running in the background. Don't write any kind of code which involves messages boxes, forms and other model UI elements.

Step 2: Building the service

From the design mode of the Windows service, right click, and from the menu select "AddInstaller," This will add two components on the designer, which will be used to install the service in the Service Control manager. Right click on the serviceprocessinstaller1 component and set the Account Property to "LocalSystem." And build the solution.

Step 3: Installing the service

After building the service, the next step is to install the service. This is done using the InstallUtil tool provided by Microsoft. Go to the bin folder of the WinService project in your HDD. You should find WindowsService1.exe. Use the installutil.exe tool as InstallUtil WindowsService1.exe. This will install the WindowsService1 as a service.

If you open the Service manager, you will be able to see Service1 in the list of services. You can manipulate the service as any other service by starting, stopping, setting the properties, etc.

That's it folks.

Source: DotNetExtreme.com

This was first published in March 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.