Monitor Live Windows Service Status / Update Service Status

  • Thread starter Thread starter B. Karttas
  • Start date Start date
B

B. Karttas

Guest
Hi,

I'd like to write a small Windows Form Application in c# to monitor a Service.

I've got it running so far. But If the Windows Service stops - In the App it'll still show "running". How could I realize it to automatically update the service.status in the textboxes without using a Button to call a method ?


using System;
using System.Windows.Forms;
using System.ServiceProcess;

namespace ESB_Info
{
public partial class MainForm : Form
{
public MainForm()
{
// The InitializeComponent() call is required for Windows Forms designer support.
InitializeComponent();
textBox1.Text = GetServiceDescription("PrintNotify");
textBox2.Text = GetServiceDescription("Spooler");
textBox3.Text = GetWindowsServiceStatus("PrintNotify");
textBox4.Text = GetWindowsServiceStatus("Spooler");

}
//
// TODO: Add constructor code after the InitializeComponent() call.
//
public static String GetWindowsServiceStatus(String SERVICENAME)
{
ServiceController sc = new ServiceController(SERVICENAME);
switch (sc.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Changing";
}
}
public static String GetServiceDescription(string SERVICENAME)
{
ServiceController sc = new ServiceController(SERVICENAME);
return sc.DisplayName;
}
}
}

If I stopp a Service, it'll still show the Status from when the Application was started.

In this case I've just use the PrintNotifier and Spooler service for test purposes.


Looking forward to find a solution :)


Cheers

Continue reading...
 
Back
Top