Working with Windows Service in C# .net

Ratings:
(4)
Views: 522
Banner-Img
Share this blog:

 

Working with Windows Service:-

  1. Service means a program.

A program, which is under the control of windows OS is called as Windows Service.

  1. Windows Service is used to develop Background processes.
  2. Windows Service contains only Application logic BUT NOT GUI.
  3. The controls which are not visible at run time can be placed in the Windows Service project.

     Ex:- Timer, Event log, File System Watcher

  1. To develop Windows Service, .net introduced

File arrrowNewarrrowProjectarrrowVisual C #  arrrow Windows arrrow Windows Service project template.  

Note:- Windows Service project template is available in Professional Edition. it is not available in  Standard Edition.

  1. To work with Windows Service, .net introduced System. Service Process Assembly.
  2. Windows Service are not Executable directly by pressing F5
  3. Windows Service is a class, which must be inherited from the Service base class.

  Screenshot_5      

  1. 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.

  1. All the Windows Service will be located at

Start arrrowSettings  arrrowControl panel arrrowPerformance and Maintenance  arrrowAdministrative tools arrrowServices   (or) Startarrrow Run  arrrow Service. MSC

  1. 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”

OBS:-  

  1. By default Message Box class is not in the scope of Windows service Project, hence System Windows. Forms assembly needs to be added.
  2. The default Timer control is not supported by Windows service.
  3. Especially for Windows service, a new Timer Control is developed, which needs to be added into the toolbox.
  4. 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 arrrow Add Reference arrrow Windows.Forms

Adding Timer Control into tool Box.  

Open Tool Box

arrrow Right click inside of General Tab

arrrow  choose Items

arrrow 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”)

{

P[i].KILL();

Message Box.Show (“Your System is effected with virus please shutdown”);

Break;

}

}

//for   Code for On Start ()

Event

{

Timer 1.Enabled = true;

}

Code for On Stop () Event

{

Timer 1.Enabled = false;

}  

Adding the installers:-

  Screenshot_6      

Build the Project  (build  menu   arrrow Build Solution )

 OBS:-

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 arrrow run  arrrowservice.MSC)

  1. right-click on ABC 1 arrrowProperties arrrow login arrrowcheck “Allow Service to interact with desktop” checkbox  arrrow ok

Note:- This step is required only when Service Contains a Message box.

  1. Right-click on ABC1 arrrow Start

Adding Installers:-

Open Service 1. Cs [Design]

arrrow Right-click inside the body

arrrow Add Installer

OBS:-

Two controls will be added Service Process Installer

arrrowProperties arrrowAccount  = Local System Service Installer  arrrowproperties arrrow Service name = abc 2 Build the project  (Build arrrow 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 arrrow run arrrowservices.msc) Sight click on ABC 2 arrrowstart Then check C:/INET PUB/mail Root/Queue folder for a mail at 12:30

  • A program to Access and Controlling Windows Service Applications.

  Screenshot_7  

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

  1. Get Service (); // Returns all the Service names from Local computer.
  2. Get Services (computer name); //Returns all the Service names from Specified computer
  3. Start ()
  4. Stop ()
  5. Service name
  • Open WEAP
  • Place two buttons and a List box as shown

  Project Menuarrrow Add Reference arrrow 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].Stop();

}

  (Or)

X [I = List Box 1. Selected Index]  .Stop ();  

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox