监视实时Windows服务状态/更新服务状态 [英] Monitor Live Windows Service Status / Update Service Status

查看:115
本文介绍了监视实时Windows服务状态/更新服务状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想在c#中编写一个小型Windows窗体应用程序来监控服务。


我'到目前为止它已经运行了。但是如果Windows服务停止 - 在应用程序中它仍将显示"正在运行"。如何才能实现自动更新文本框中的service.status而不使用Button来调用方法?


使用System; 
使用System.Windows.Forms;使用System.ServiceProcess
;

namespace ESB_Info
{
public partial class MainForm:Form
{
public MainForm()
{
// The Windows窗体设计器支持需要InitializeComponent()调用。
InitializeComponent();
textBox1.Text = GetServiceDescription(" PrintNotify");
textBox2.Text = GetServiceDescription(" Spooler");
textBox3.Text = GetWindowsServiceStatus(" PrintNotify");
textBox4.Text = GetWindowsServiceStatus(" Spooler");

}
//
// TODO:在InitializeComponent()调用后添加构造函数代码。
//
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" ;;
默认值:
return" Status Changing" ;;
}
}
public static String GetServiceDescription(string SERVICENAME)
{
ServiceController sc = new ServiceController(SERVICENAME);
返回sc.DisplayName;
}
}
}

如果我停止服务,它仍会显示应用程序启动时的状态。 / p>

在这种情况下,我只是使用PrintNotifier和Spooler服务进行测试。



寻找前进找到解决方案:)



干杯

解决方案

您好,


根据Castorix31对WaitForStatus的推荐,请参阅以下示例。

 

https://github.com/karenpayneoregon/WindowsInstaller/blob/master/ServiceInstaller/Classes/WindowsServices.cs#L59


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

解决方案

Hello,

As per Castorix31 recommendation for WaitForStatus, see the following for an example.

https://github.com/karenpayneoregon/WindowsInstaller/blob/master/ServiceInstaller/Classes/WindowsServices.cs#L59


这篇关于监视实时Windows服务状态/更新服务状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆