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.
Like this:
Like Loading...
Search Particular Text used in Stored Procedure in Sql Server
Posted by Viral Sarvaiya on September 10, 2012
Few days ago I get very tedious job in database,
I have to search all stored procedure which have use one particular table.
I get following 1 option of query from sys table of sqlserver
but this query only table table’s name, if We want particular string not table then this query will fail so below stored procedure will work.
Run this query as like below
You will get require output of list of stored procedure name.
In this Stored Procedure i take one output parameter for check number of rows return by Stored Procedure.
Hope this will help you.
Thanks.
Share this:
Like this:
Posted in Sql Server | Tagged: CREATE PROCEDURE, INFORMATION_SCHEMA.ROUTINES, Pl/Sql, Search Particular Text used in Stored Procedure in Sql Server, SQL, Sql Server, Stored procedure, sys.procedures, sys.Tables, syscomments, sysobjects, T SQL | 1 Comment »