Home arrow VB.Net arrow Page 3 - Creating A Windows Service in VB.NET

Creating A Windows Service in VB.NET

Up until .Net, the creation of Windows services was limited to C++ guru's. Not anymore. In this article Jayesh shows us how to create, install, start and stop a service with VB.Net.

Author Info:
By: Jayesh Jain
Rating: 4 stars4 stars4 stars4 stars4 stars / 305
October 04, 2002
  1. · Creating A Windows Service in VB.NET
  2. · What is Windows Service
  3. · Adding an Installer to the Project
  4. · Conclusion

print this article

Creating A Windows Service in VB.NET - Adding an Installer to the Project
(Page 3 of 4 )

Open the service1.vb design window, right click on it and select Add Installer option, which will add an installer project (called ProjectInstaller.vb) with two controls -- ServiceProcessInstaller1 and ServiceInstaller1 -- to our existing project.

Select the ServiceInstaller1 control and open the property window. Change the ServiceName property and DisplayName property to MyService (this is the name you want to appear in the list of services in the services window).

Select the ServiceProcessInstaller1 control and open the property window. Change the Account property to LocalSystem (this needs to be specified as we need to run the service on our local machine).

Now it's time for us to build the application and create an executable. Select Build Solution from the Build menu to create an executable with installation instructions for the service.

Installing the Service
To install our service we need to use the InstallUtil program, which is a .NET utility to install Windows services. You can it in C:\ WINNT\ Microsoft.NET\ Framework\ v1.0.3705

This directory might be different on your computer, depending upon the version of the .NET framework you are working with.

Alternatively, you could run a .NET Command Window by selecting Start > Programs -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools >- Visual Studio .NET Command Prompt, which sets all of the required paths for you.

Type the following command in the command window:

InstallUtil C:\tgol\Myservice\Bin\Myservice.exe

Tthis is the path for the executable of the service we just created. Remember that VB.NET created the executable in the Bin folder under the project folder, so make sure you change this to your executable path.

Starting the Service
Running a service and starting a service are two different things -- when you install the service with InstallUtil you are running the service, but have yet have to start it.

To view and start the service, open Control Panel -> Administrative Tools. Now click Services, locate MyService, right click on it and select Start to start it:

Our service is now started. Open the Event Viewer from Administrative Tools and click Application Log to see the logs created by the Service (MyService) every 10 seconds. If you don't see any logs click refresh (F5). You will have to keep refreshing to see the latest event logs:

Stopping the Service
This procedure is similar to installing the service but now we shall run the InstallUtil with the /U parameter, which will uninstall the service:

InstallUtil /U C:\tgol\Myservice\Bin\Myservice.exe (or the executable path on your computer).

Take note of the message to confirm that the service was uninstalled properly.

  • Stop the service and close the service window before you install/uninstall the service.
  • Always uninstall/install if you make any changes to the service application.
  • Try avoiding a user interface, inputs and message boxes in the service application.
  • Open ProjectInstaller.vb, select the ServiceInstaller1 control, and open the property window. Change the StartType property to automatic if you want to start the service automatically.

blog comments powered by Disqus

- MyClass - Implementing Polymorphism in VB.Net
- Building a News Ticker Using VB.Net
- Everything You Wanted to Know About Forms In...
- Building Assemblies with VB.Net
- Simple VB.NET Notify Icon with Panel Applica...
- Regular Expressions in .NET
- String Encryption With Visual Basic .NET
- Deploying Applications in VB.NET: Part 1/2
- Watching Folder Activity in VB.NET
- Creating A Windows Service in VB.NET
- Implementing The Google Web Service In VB.NET
- Migrating to Visual Basic.NET from Visual Ba...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials