【发布时间】:2010-03-06 13:44:19
【问题描述】:
我使用 ServiceControllerStatus 获得了启动和启动状态的运行状态。 有什么方法可以查看服务是否已经完全启动
【问题讨论】:
我使用 ServiceControllerStatus 获得了启动和启动状态的运行状态。 有什么方法可以查看服务是否已经完全启动
【问题讨论】:
ServiceController.Status() 属性已使用本机 QueryServiceStatus() API 函数。如果服务返回 SERVICE_START_PENDING 状态码,它将返回 ServiceControllerStatus.StartPending。
您必须按原样获取返回值,服务完全控制其状态代码。如果您从未获得 StartPending,那么该服务很可能不需要任何时间即可开始。这是相当普遍的。如果您发现服务有一段时间没有响应,即使您获得了 Running 状态,也说明服务实现存在缺陷。
【讨论】:
是的,但是有点丑:
导入 ADVAPI32.DLL:
[DllImport ("advapi32.dll", EntryPoint = "QueryServiceStatus", CharSet = CharSet.Auto)]
internal static extern bool QueryServiceStatus (IntPtr hService, ref SERVICE_STATUS dwServiceStatus);
声明一些结构体:
[StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
public int serviceType;
public int currentState;
public int controlsAccepted;
public int win32ExitCode;
public int serviceSpecificExitCode;
public int checkPoint;
public int waitHint;
}
public enum State
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
SERVICE_RUNNING = 0x00000004,
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007,
}
编辑:用户QueryServiceStatus() 更容易,但总的来说它保持不变。详情见链接,我觉得这里贴那么多代码没什么用。
然后您可以使用ControlService 和控制代码SERVICE_CONTROL_INTERROGATE 来发出状态信息请求。
看 http://msdn.microsoft.com/en-us/library/ms682108(VS.85).aspx
【讨论】: