In some cases you may want to have an application which is performing since the computer has been turned on. For instance consider an application which is logging changes in an specific directory in your server, or you might think about Anti Viruses or any other application like that.
Windows Services a kind of windows application without any user interface, which is working in background of your system. In many situation you may want to have an application which is working on your system, event before any user has been logged in.
There is a class names ServiceBase in System.ServiceProcess namespace which has to be derived if you want to have your own Windows Service application. The ServiceBase classs has OnStart() method which will occur when the service has been started using SCM (Service Control Manager) which is accessible by Services icon on Administrative Tools of Control Panel. You can override the OnStart() method for doing any special task in your service. You can also stop your service tasks by overriding the OnStop() method of ServiceBase.
Consider that every service class needs an Service Installer to install the service. So for creating the service installer just goto design mode of your service class and Right Click on it and then click on Add Installer then you will see that a new class has been added to your project. Consider that your services will be run with an specific User of system, to change this behavior you have to change the Account property of ServiceProcessIntaller instance in the new added class to LocalSystem. In the next post I will talk more about other accounts and properties of this class.
َFor running a service you SCM first you have to install. To achieve this goal you can use the InstallUtil application with your assembly (the exe file) passed as parameter. You may also want to uninstall a service which can be done by passing /U at the end of InstallUtil.
I've prepared a sample which will log all the changes in a specified directory within the configuration file. The log file path is also can be set within the configuration file.
You can download the source code here:
http://www.tabatabaei.info/csharpsamples/systemwatcher.rar
Sunday, August 19, 2007
Windows Service Applications
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment