Group Discounts available for 3+ students and Corporate Clients

Working with Windows Service in C# .net

Working with Windows Service:-

  1. Service means a program.

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

  1. Windows Service are used to develop Back ground 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 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 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.

 

  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 accessibility of calculator software , if user trying to open  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 need 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 need to be added in to tool box.
  4. 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 arrrow Add Reference arrrow Windows .Forms

Adding Timer Control  in to 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 desk top” check box  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, service controller class is required , which is the part of Service process Assembly.

Methods of 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. Serveice 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 ();

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Working with Windows Service in C# .net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please Enter Your Details and Query.
    Three + 6