Working with Windows Service:-
- Service means a program.
A program, which is under the control of windows OS is called as Windows Service.
- Windows Service is used to develop Background processes.
- Windows Service contains only Application logic BUT NOT GUI.
- The controls which are not visible at run time can be placed in the Windows Service project.
Ex:- Timer, Event log, File System Watcher
- To develop Windows Service, .net introduced
File NewProjectVisual C # Windows Windows Service project template.
Note:- 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 the 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.
Interested in mastering .NET? Learn more about ".NET Training" in this blog post.
- All the Windows Service will be located at
Start Settings Control panel Performance and Maintenance Administrative tools Services (or) 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 the accessibility of calculator software, if the user trying to open the 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 needs to be added.
- The default Timer control is not supported by Windows service.
- Especially for Windows service, a new Timer Control is developed, which needs to be added into the toolbox.
- When software is opened, then that software’s exe file name is added into processes (Ctrl + alt + del à Processes)
Open windows service project with project name WSI
- Project menu Add Reference Windows.Forms
Adding Timer Control into tool Box.
Open Tool Box
Right click inside of General Tab
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”);
//for Code for On Start ()
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 desktop” checkbox ok
Note:- 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
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) OBS:- 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, a service controller class is required, which is the part of the Service process Assembly. Methods of the 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 ()
- Service 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 ();