Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Windows Services for VB.NET Developers
 »  Home  »  Windows Development  »  Windows Services for VB.NET Developers
Windows Services for VB.NET Developers
by Scott Rutherford | Published  12/20/2005 | .NET Newbie Windows Development | Rating:
Scott Rutherford
Scott Rutherford is a consultant and .NET developer who has developed Enterprise business applications in many fields including Restaurant, Market Research, Automotive, and Analytical Chemistry. He is an Microsoft Certified Professional with the .NET Platform. 

View all articles by Scott Rutherford...
Introduction

A Windows Service is an application that runs outside of any desktop session. It can run automatically when Windows starts up, before any users log in. Alternatively, it can be started up by some other app via the Windows Service Control Manager (SCM - pronounced "scum"). Not to be confused with a Web Service, which runs a particular protocol and serves up XML to remote machines, a Windows Service is a basic building block of the Windows operating system. An out-of-the-box install of Windows XP has more than 80 Windows Services installed (necessary?). Outside of a Microsoft environment, this type of program is commonly referred to as a ‘daemon’, or more accurately, a ‘dragon’ (which appeals far more to the author than any other terminology). It runs in a security context independent of any logged on user—configured in the Services Manager (right-click My Computer, select Manage, expand Services and Applications, then Services) where you’ll also find its start/stop controls.

Visual Studio .NET makes it very simple to create and install Windows Services: just select the Windows Service template when starting a new project. In this article you’ll see the (very little) code needed to make this service work and all the Administrative tasks you’ll need to perform to get it running. You’ll build a project called “Empty Service” that can be used as a template to build all kinds of Windows Services. You then just add your own business class to perform the activity you need.

A Windows Service exposes two main commands: Start and Stop. It begins work when it receives a Start command from the SCM. It is therefore in the OnStart method that you will put code to execute whatever work it is you want the service to perform. The Service can support other functions optionally, such as Pause and Continue, or Custom Commands. These too respond to controls received from the SCM. You want to avoid any desktop interaction since your service will run in a security session unattached to any particular user session (e.g. no message boxes, prompts, etc.).

As the Start command is only called once, the Service itself must implement a mechanism to periodically “wake the dragon”. The simplest way to do this is to drop a Timer component on the Service designer, and call its Start and Stop methods from the OnStart and OnStop methods of your Service. However, this gives you little control over process creation and you end up with many threads running at a time. This article shows you how to implement a single “worker” thread which executes in a loop only when the previous iteration is complete—hence program overlap is avoided.

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 01/04/2006)
Rating
Great!
 
Comment #2  (Posted by Shihab on 01/25/2006)
Rating
Very useful
 
Comment #3  (Posted by an unknown user on 01/28/2006)
Rating
I have been looking for an intro on Windows services. Thanks!
 
Comment #4  (Posted by an unknown user on 03/06/2006)
Rating
good one
 
Comment #5  (Posted by an unknown user on 03/23/2006)
Rating
Nice work!
 
Comment #6  (Posted by an unknown user on 03/23/2006)
Rating
Nice work!
 
Comment #7  (Posted by an unknown user on 03/28/2006)
Rating
Quite interesting.
 
Comment #8  (Posted by an unknown user on 03/29/2006)
Rating
good
 
Comment #9  (Posted by an unknown user on 03/29/2006)
Rating
good
 
Comment #10  (Posted by an unknown user on 04/28/2006)
Rating
Great Intro
 
Comment #11  (Posted by an unknown user on 05/15/2006)
Rating
Really great work. Thanks

I wonder if a windows service can consume a web service too ?
 
Comment #12  (Posted by an unknown user on 05/29/2006)
Rating
Sure a Service can consume a web service like any other Windows app. You need to ensure your security context will work -- i.e. the account that the Windows Service runs as must have ability to make web request (authenticate if necessary to web server, on proxy, etc.).
 
Comment #13  (Posted by an unknown user on 06/07/2006)
Rating
Great explanation
 
Comment #14  (Posted by an unknown user on 06/07/2006)
Rating
excellent! exactly what I needed.
 
Comment #15  (Posted by an unknown user on 07/18/2006)
Rating
most informative article i've found so far - at least it explains what i'm trying to achieve. shame the example is in vb though, such an ugly language. wish i could find more c# centred stuff...
 
Comment #16  (Posted by an unknown user on 09/14/2006)
Rating
Excellent!
 
Comment #17  (Posted by an unknown user on 09/15/2006)
Rating
Nice work!
 
Comment #18  (Posted by an unknown user on 10/02/2006)
Rating
Hi,
Nice project !
You can also uninstall a service using c:\winnt\SC.exe delete service1
Sc is part of the W2k Resource kit.
http://www.petri.co.il/download_free_reskit_tools.htm

Andy

 
Comment #19  (Posted by an unknown user on 10/23/2006)
Rating
I reviewed a dozen articles on the subject of building a service in vb.net, and this is the only one that actually works! Thanks a bunch Scott.
 
Comment #20  (Posted by an unknown user on 11/16/2006)
Rating
It works in 10 minutes :-)) Thanks
 
Comment #21  (Posted by an unknown user on 11/27/2006)
Rating
Just excellent! Thank you Scott
 
Comment #22  (Posted by Larry on 11/28/2006)
Rating
Hi, I cannot install this service. I compiled the code form this article and receiving an error message durring installaion: Unknown error "-1". Please help.

Thanks,
Larry
 
Comment #23  (Posted by an unknown user on 01/04/2007)
Rating
Excelent and simple! Thanks a lot.
 
Comment #24  (Posted by an unknown user on 01/18/2007)
Rating
Simple and to the point. Thank you.
 
Comment #25  (Posted by Doug on 01/23/2007)
Rating
As I have the "standard" edition, I can't seem to get this to work. It compiles & installs, but won't start, *immediately* throwing the "failed to start in a timely fashion."
What type of project must one start with, what does the project's property page look like?
 
Comment #26  (Posted by an unknown user on 02/12/2007)
Rating
what a childice example have you given.
 
Comment #27  (Posted by an unknown user on 02/19/2007)
Rating
How about some hint of "How to consume a win service/service Method form an application??
 
Comment #28  (Posted by [The author] on 02/19/2007)
Rating
RE; Comment #27: The article does present the concept of "OnCustomCommand" on page three. However, from your language I expect you are talking about Web Services which are clearly distinguished from Windows Services on page one.
 
Comment #29  (Posted by an unknown user on 03/12/2007)
Rating
Very helpful. Thanks!
 
Comment #30  (Posted by an unknown user on 05/03/2007)
Rating
I was looking for that installutil thingy... Thanks for the great input!
 
Comment #31  (Posted by an unknown user on 06/05/2007)
Rating
WS with timer explaination are many, with threads its really rare. This one is a good one out of those rare explainations.
 
Comment #32  (Posted by an unknown user on 06/06/2007)
Rating
Really good but needed debug process for development environment too.
 
Comment #33  (Posted by David on 06/07/2007)
Rating
Excellent explanations along with working code. The only questions I have left are how to get an icon in the system tray for this service and how to display a popup menu on a right click of that icon.
 
Comment #34  (Posted by an unknown user on 06/30/2007)
Rating
Thanks
 
Comment #35  (Posted by an unknown user on 07/21/2007)
Rating
Excellent understood very well keep it up and give us nice articles such as this
 
Comment #36  (Posted by Aakash on 09/06/2007)
Rating
This is my first exposure to Windows Services and the content was delivered very nicely, good work Scott.

(Comment #15) I do not digest why people hate VB, is it becuase it does those great things in simple manner where as its tough to achieve the same in their so called very good language ??
 
Comment #37  (Posted by Wardell Dye on 09/07/2007)
Rating
This was an excellent introduction to Windows Services as I was just given an assignment to preform a task to use Windows Services, great job!!!!!

Thanks
 
Comment #38  (Posted by an unknown user on 10/10/2007)
Rating
Perhaps I am missing something, but when I try to compile the code, I'm getting the following error: 'Sub Main' was not found in 'WindowsService1.Service1'. Do I need to change the Startup Object?

 
Comment #39  (Posted by an unknown user on 10/17/2007)
Rating
Compiled OK. Installed OK. Service Started...BUT...nothing in the event viewer when I start the service...
 
Comment #40  (Posted by Bobby on 11/29/2007)
Rating
When I try to start the service, I get this error. Any idea as to what could be happening?

Service cannot be started. System.ArgumentException: Delegate to an instance method cannot have null 'this'.
at System.MulticastDelegate.ThrowNullThisInDelegateToInstance()
at WindowsService.Service.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback
 
Comment #41  (Posted by Bobby on 11/29/2007)
Rating
Sorry, my mistake with above error. Works perfectly now. Thanks a lot.
 
Comment #42  (Posted by an unknown user on 12/10/2007)
Rating
Followed instructions and re-checked. Service will not install, so something must have been left out

 
Comment #43  (Posted by an unknown user on 02/19/2008)
Rating
Great sample. Following your instructions it works at first execution. Thanks very much !!!
 
Comment #44  (Posted by an unknown user on 03/26/2008)
Rating
it worked as per my requirement..
 
Sponsored Links