简体   繁体   English

检查服务是否正在使用Win32_Service运行

[英]Check if a service is running with Win32_Service

I have a very quick question about Win32_Service 我对Win32_Service有一个非常快速的问题

Is

String sState = service["State"].ToString();
if (! sState.Equals("Running"))

Sufficent to moniter if a service is not running? 如果服务未运行,足够监视吗? I want the if statement to return true in all cases where the service isn't running so it can send a warning message. 我希望if语句在服务未运行的所有情况下都返回true,以便它可以发送警告消息。

Windows service states are: Windows服务状态为:

  • SERVICE_STOPPED . SERVICE_STOPPED Service is not running. 服务未运行。
  • SERVICE_START_PENDING . SERVICE_START_PENDING Service is starting (but you can assume that it is not running. Hopefully the service will start soon) 服务正在启动(但是您可以假设它没有运行。希望该服务将很快启动)
  • SERVICE_STOP_PENDING . SERVICE_STOP_PENDING Service is stopping. 服务正在停止。 (but you can assume that it is still running, except possibly the only thing service is doing is finishing in an orderly manner, disregarding any external request) (但是您可以假定它仍在运行,除非服务唯一要做的是有条不紊地完成操作,而不考虑任何外部请求)
  • SERVICE_RUNNING . SERVICE_RUNNING Service is running (and ready to meet external requests) 服务正在运行(并准备满足外部请求)
  • SERVICE_CONTINUE_PENDING . SERVICE_CONTINUE_PENDING Similar to START_PENDING . START_PENDING类似。 Hopefully the service will start soon. 希望该服务将很快开始。
  • SERVICE_PAUSE_PENDING . SERVICE_PAUSE_PENDING Service is going to be paused in the near future. 服务将在不久的将来暂停。
  • SERVICE_PAUSED . SERVICE_PAUSED Service is paused 服务已暂停

The only state that guarantees that the service is running is SERVICE_RUNNING , but I would not display a warning if the service is in state START_PENDING . 保证服务正在运行的唯一状态是SERVICE_RUNNING ,但是如果服务处于状态START_PENDING则不会显示警告。 At least, show different warnings depending on the state. 至少,根据状态显示不同的警告。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM