Table of Contents
Figure 1: Windows service application controller.
Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.
Reference - MSDN
In this article, we are going to create an application to get a list of service applications currently installed with the status and how to control (i.e., we can simultaneously Stop or Start multiple Services) each service application in your local computer.
Windows service application is often used for various purposes like push pull data transfer, email, system monitor, etc. My concern is how to manage all the customized service applications in a local server / a remote server as well, that’s why I need to create tools where I can get all the status of specified service applications on a local / remote computer.
There are two types of services you can create in Visual Studio using the .NET Framework.
- Win32OwnProcess: Services that are the only service in a process are assigned the type
Win32OwnProcess
.
- Win32ShareProcess: Services that share a process with another service are assigned the type
Win32ShareProcess
.
*Note: You can retrieve the service type by querying the ServiceType
property.
You might occasionally see other service types if you query existing services that were not created in Visual Studio. For more information on these, see the ServiceType
.
Microsoft Visual Studio .NET Framework 2.0 / update version provides the namespace System.ServiceProcess
and we are going to use ServiceController
class found in the mentioned namespace. This class represents a Windows Service and allows you to connect to a Windows Service, to manipulate the service and get information about the service.
You can download the sample source code to get a complete picture of the program.
public partial class Manager : Form
{
public string strServerName = "Local";
class ServiceInfo
{
private string strName, strStatus;
public ServiceInfo(string sName, string sStatus)
{
strName = sName;
strStatus = sStatus;
}
public string ServiceName
{
get { return strName; }
}
public string ServiceStatus
{
get { return strStatus; }
}
}
private ArrayList ALWServiceInfo;
public Manager()
{
InitializeComponent();
ALWServiceInfo = new ArrayList();
}
private void Manager_Load(object sender, EventArgs e)
{
}
private void getWindowsServices()
{
ServiceController[] objArrServices;
try
{
if ((this.comboBoxServerNameOrIPs.Text == "Local")
|| this.comboBoxServerNameOrIPs.Text ==
System.Environment.MachineName.ToString ()
|| this.comboBoxServerNameOrIPs.Text == "127.0.0.1")
{
objArrServices = ServiceController.GetServices();
}
else
objArrServices = ServiceController.GetServices
(comboBoxServerNameOrIPs.Text);
ALWServiceInfo.Clear();
foreach (ServiceController objServiceController in objArrServices)
{
ALWServiceInfo.Add(new ServiceInfo
(objServiceController.ServiceName,
objServiceController.Status.ToString()));
}
dgServices.DataSource = ALWServiceInfo;
dgServices.Refresh();
}
catch (Exception ex)
{
MessageBox.Show("Error Occurred: " + ex.Message);
}
}
private void buttonConnect_Click(object sender, EventArgs e)
{
this.getWindowsServices();
}
private void buttonClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void setCommandButton()
{
string strServerStatus = WSControllerAgent.Status.ToString();
if (strServerStatus == "Running")
{
if (WSControllerAgent.CanPauseAndContinue == true)
{
ButtonPause.Enabled = true;
}
else
{
ButtonPause.Enabled = false;
}
ButtonStop.Enabled = true;
ButtonStart.Enabled = false;
}
else if (strServerStatus == "Paused")
{
ButtonStart.Enabled = true;
ButtonPause.Enabled = false;
ButtonStop.Enabled = true;
}
else if (strServerStatus == "Stopped")
{
ButtonStart.Enabled = true;
ButtonPause.Enabled = false;
ButtonStop.Enabled = false;
}
}
private void getSelectedServiceStatus()
{
try
{
}
catch (Exception ex)
{
MessageBox.Show("Error Occurred: " + ex.Message);
}
}
private void dgServices_MouseClick(object sender, MouseEventArgs e)
{
this.onLoadWServices(this.dgServices.CurrentCell.Value.ToString());
}
private void onLoadWServices(string strServiceName)
{
try
{
ServiceController[] AvailableServices =
ServiceController.GetServices(".");
if (strServiceName != "")
{
foreach (ServiceController AvailableService in AvailableServices)
{
if (AvailableService.ServiceName == strServiceName)
{
this.WSControllerAgent.ServiceName = strServiceName;
this.setCommandButton();
return;
}
}
}
}
catch
{
MessageBox.Show("The Service is not installed on this Machine",
"Service is not available");
this.Close();
Application.Exit();
}
}
private void ButtonStart_Click(object sender, EventArgs e)
{
if (WSControllerAgent.Status.ToString() == "Paused")
{
WSControllerAgent.Continue();
}
else if (WSControllerAgent.Status.ToString() == "Stopped")
{
ServiceController[] ParentServices = WSControllerAgent.ServicesDependedOn;
try
{
if (ParentServices.Length >= 1)
{
foreach (ServiceController ParentService in ParentServices)
{
if (ParentService.Status.ToString() != "Running" ||
ParentService.Status.ToString() != "Paused")
{
if (MessageBox.Show("This service is required.
Would you like to also start this service?\n" +
ParentService.DisplayName, "Required Service",
MessageBoxButtons.YesNo).ToString() == "Yes")
{
ParentService.Start();
ParentService.WaitForStatus
(ServiceControllerStatus.Running);
}
else
{
return;
}
}
}
}
WSControllerAgent.Start();
WSControllerAgent.WaitForStatus
(System.ServiceProcess.ServiceControllerStatus.Running);
}
catch (Exception ex)
{
MessageBox.Show("Error Occurred: " + ex.Message.ToString());
}
this.setCommandButton();
}
}
private void ButtonPause_Click(object sender, EventArgs e)
{
if (WSControllerAgent.CanPauseAndContinue == true)
{
if (WSControllerAgent.Status.ToString() == "Running")
{
WSControllerAgent.Pause();
}
WSControllerAgent.WaitForStatus
(System.ServiceProcess.ServiceControllerStatus.Paused);
this.setCommandButton();
}
}
private void ButtonStop_Click(object sender, EventArgs e)
{
if (WSControllerAgent.CanStop == true)
{
ServiceController[] DependentServices =
WSControllerAgent.DependentServices;
if (DependentServices.Length >= 1)
{
foreach (ServiceController DependentService in DependentServices)
{
if (DependentService.Status.ToString() != "Stopped")
{
if (MessageBox.Show("Would you like to also
stop this dependent service?\n"
+ DependentService.DisplayName
, "Dependent Service"
, MessageBoxButtons.YesNo).ToString() == "Yes")
{
DependentService.Stop();
DependentService.WaitForStatus
(ServiceControllerStatus.Stopped);
}
else
{
return;
}
}
}
}
if (WSControllerAgent.Status.ToString() ==
"Running" || WSControllerAgent.Status.ToString() == "Paused")
{
WSControllerAgent.Stop();
}
WSControllerAgent.WaitForStatus
(System.ServiceProcess.ServiceControllerStatus.Stopped);
this.setCommandButton();
}
}
}
I hope it might be helpful to you. Enjoy!
- 5th August 2009: Initial post