Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for November, 2012

Handling Window services thru C#

Posted by Viral Sarvaiya on November 23, 2012


Hello.

Open Run window form start menu or Press Window key + R, Run window will open, in textbox type “services.msc” and Hit Enter, Service window will open.

Service window has list of the services installed in the computer.

To these all service we can call Window service in technical word.

Window service enable you to create long-running executable applications.

To know how to create window service please click here

C# provide you functionality to handle installed services, that will include in namespace System.ServiceProcess

Here i am explain, how to handle window service.

1. To get list of the Window service.

You can get list of the window service installed in computer from below code,

In System.ServiceProcess namespace we have a from that we can get list of the window service.


ServiceController[] ListServices = ServiceController.GetServices();

2. Search particular service from list.


ServiceController[] ListServices = ServiceController.GetServices();

string TextService = "My Sample Service"

if (LstServices.Where(a => a.ServiceName == TextService).Count() > 0)
{
ServiceController Myservice = new ServiceController(TextService);
}
else
{
MessageBox.Show(TextService + " is not Installed");
}

ServiceController.GetServices() gives list of the service so i take that in array of the ServiceController class.

And from linq query we can check that particular service is in the list or not and if that list count is greater than zero then we get that service is installed in the computer else not.

And to get that particular service, here i have creating object of the ServiceController class.

3. Check the status of the Service.

If the window service is installed in computer then we can also check the status of that window service.

Now we assume that we find the service “My Sample Service”.

So from below code we can get the status of the service whether it is Running, Paused, Stopped.


ServiceControllerStatus ServiceStatus = Myservice.Status;

ServiceControllerStatus is Enum for all service statuses.

If we want to compare status then we can use as like below.


if (ServiceStatus == ServiceControllerStatus.Running)
{
//Do something
}

4. Start/Stop the Service

From ServiceController class we can start or stop the particular Service with object of the ServiceController.

To start the service you can use


Myservice.Start();

And to stop the service you can use


Myservice.Stop();

5. To Restart the Service

To restart the service first we have to check that service is running or not and if it is running then we can stop the service and start the service.


ServiceController[] ListServices = ServiceController.GetServices();

string TextService = "My Sample Service"

if (LstServices.Where(a => a.ServiceName == TextService).Count() > 0)
{
ServiceController Myservice = new ServiceController(TextService);

ServiceControllerStatus ServiceStatus = Myservice.Status;

if (ServiceStatus == ServiceControllerStatus.Running)
{
Myservice.Stop();

TimeSpan timeout = TimeSpan.FromMilliseconds(2000);

Myservice.WaitForStatus(ServiceControllerStatus.Stopped, timeout);  //To wait 2 second for Stop Service

Myservice.Start();

Myservice.WaitForStatus(ServiceControllerStatus.Running, timeout);   // To wait 2 second for Start Service
}
}
else
{
MessageBox.Show(TextService + " is not Installed");
}

hope this will helpful

Thanks.

Posted in .Net, Window Service | Tagged: , , , , , , , , , , | Leave a Comment »

 
%d bloggers like this: