Working with Windows Service in C# .net
Working with Windows Service:-
- Service means a program.
A program, which is under control of windows OS is called as Windows Service.
- Windows Service are used to develop Back ground processes.
- Windows Service contains only Application logic BUT NOT GUI.
- The controls which are not visible at run time can be placed in Windows Service project.
Ex :- Timer, Event log, File System Watcher
- To develop Windows Service, .net introduced
File NewProjectVisual C # Windows Windows Service project template.
Windows Service project template is available in Professional Edition . it is not available in Standard Edition.
- To work with Windows Service, .net introduced System. Service Process Assembly.
- Windows Service are not Executable directly by pressing F5
- Windows Service is a class, which must be inherited from Service base class.
- Every Windows Service by default hare two events
(a) On Start () will be executed when service is about to start.
(b)On Stop () will be executed when service is about to stop.
- All the Windows Service will be located at
Start Settings Control panel Performance and Maintenance Administrative tools Services
Start Run Service. MSC
- Steps for developing windows Service
Step 1: Open windows service Project Template and write the code as required.
Step 2:- Add the installers and build the project to get an Exe file.
Step 3: Install the exe file with the help of INSTALLUTIL TOOL.
Step 4 : Open Service and start the service
Example 1 on Windows Service
Develop a service to restrict accessibility of calculator software , if user trying to open calculator then display a message like “ Your System is effected with virus Please shut down”
- By default Message Box class is not in the scope of Windows service Project , hence System Windows. Forms assembly need to be added.
- The default Timer control is not supported by Windows service .
- Especially for Windows service a new Timer Control is developed, which need to be added in to tool box.
- When a software is opened, then that software’s exe file name is added in to processes (Ctrl + alt + del à Processes)
Open windows service project with project name WSI
- Project menu Add Reference Windows .Forms
Adding Timer Control in to tool Box.
Open Tool Box Right click inside of General Tab choose Items Timer (System. Timers)
Place Timer in Windows service with Enabled = false;
Using System . Windows .Forms;
Code for Timer 1_ Elapsed event
Process  p = process . Get Process ();
For (int I = 0; I < p. length; I ++)
If (p [i].Process Name . To Upper () = = “CALC”)
Message Box .Show (“Your System is effected with virus please shutdown”);
Code for On Start () Event
Timer 1.Enabled = true;
Code for On Stop () Event
Timer 1.Enabled = false;
Adding the installers:-
Build the Project (build menu Build Solution )
WS1 .exe is created under
D: / C194 / WS1 / Bin/Debug folder with a Service called as ABC 1
Installing the Service
Open.net command prompt
Debug > install util – I ws1.exe
The transacted install completed
Service is installed with the name ABC 1 Staring the service
Open Service (Start run service.MSC)
- right click on ABC 1 Properties login check “Allow Service to interact with desk top” check box ok
This step is required only when Service Contains a Message box.
- Right click on ABC1 Start
Open Service 1. Cs [Design] Right click inside the body Add Installer
Two controls will be added
Service Process Installer Properties Account = Local System
Service Installer properties Service name = abc 2
Build the project (Build build Solution)
Ws2.exe is created under
D: / C 194 /ws2/bin/Debug folder with a Service called as ABC 2
Open. Net command prompt
Change to D:/c 194 /ws2/bin/Debug folder
Install Util – I ws2.exe
Open service (Start run services.msc)
Sight click on ABC 2 start
Then check C:/INET PUB/mail Root/Queue folder for a mail at 12:30
- A program to Access and Controlling windows Service Applications.
To access the service, service controller class is required , which is the part of Service process Assembly.
Methods of service controller class
- Get Service (); // Returns all the Service names from Local computer.
- Get Services (computer name); //Returns all the Service names from Specified computer
- Start ()
- Stop ()
- Serveice name
- Open WEAP
- Place two buttons and a List box as shown
Project Menu Add Reference System. Service Process
Using System. Service Process;
Code for GD
Service Controller  x;
Code for Formal_ load event
X = Service Controller . get Service ();
For (int I = 0; I < x. length; I ++)
List Box1.Items .Add (x [i]. Service. Controller);
Code for start button
Int I = list box1. Selected Index;
X [i].Start ();
Code for stop button
Int I = list box1. Selected Index;
X [I = List Box 1. Selected Index] .Stop ();