How to Create a .NET Windows Service EasilyWindow services are the applications which are controlled and managed by Service Control Manager and generally used in servers as they are long running applications. Other interesting thing to note here is that they do not have any user interface and also they dont require any user for starting or executing them. In these article methodologies for creating .NET based Window Services will be discussed. Commonly
used method for creating .NET Windows Service is timer based approach.
Generally no user interaction is involved in these types of applications;
there is need of a timer whose task is to manage the invocation of a backstage
thread at regular intervals. For understanding this in more detail you
should know about the System.ServiceProcess.Service class and its methods.
This is the class which is extended by all the .NET based services and it has three methods which need to be overridden by the service. There are OnStart and OnStop methods which controls the starting and stopping of the service respectively. Also there is a method called Dispose which cleanup the resources which are no longer required. So in Timer approach first you should create a timer in the OnStart method and then can put your working function details under it. The working function has the code which needs to be executed at regular intervals, when the timer got activated. There are two alternatives to this method. First is single thread approach in which a separate thread is created for the invocation of working function. Also the working function will work in an endless loop till the service is running. It is suspended when the service is stopped. In the second alternative which is multiple thread approach there are multiple worker threads. It can be viewed as an array of worker threads in the background for controlling the service. With the help of .NET framework and the classes it provides, it has become quite easy to develop a window service. It was a tough task earlier as there was need to use system level procedures if you are creating a window service using C/C++. Also .NET allows creating as well as installing and controlling the window service. Visual Studio .NET is a nice IDE for creating these applications as it eases the task with its visual drag and drop controls and built in templates. There is window service project available from the project templates which can be used as a foundation of our development. Also timer is available as an object in the toolbox and it can be dragged to our project surface. So with the help of .NET framework, class libraries and the IDE, Visual Studio .NET you have got a comparatively simpler, easier and user friendly way of creating a window service.
|