这就是我得到的:
protected override void OnStart(string[] args)
{
    if (SomeApp.Initialize())
    {
        SomeApp.StartMonitorAndWork();
        base.OnStart(args);
    }
}
protected override void OnStop()
{
    SomeApp.TearDown();
    base.OnStop();
}
这里Initialize读取一个配置文件,如果有错,那就没什么可做的,所以服务应该停止!如果配置正常StartMonitorAndWork启动:
Timer(new TimerCallback(DoWork), null, startTime, loopTime);
和DoWork定期轮询数据库.
如果Initialize失败(我检查日志文件),我尝试从管理工具 - >服务停止服务,我得到:
Could not stop the SomeService on Local Computer. The service did not return an error. This could be internal Windows error or an internal service error. If the problem persists, contact system administrator.
The question is: "Is exiting OnStart without doing nothing enough if Initialize returns false?
或者应该有这样的事情: …
try
{
     object result = processClass.InvokeMethod("Create", methodArgs);
}
catch (Exception e)
{  
    // Here I was hoping to get an error code.
}
当我调用上面的WMI方法时,我应该得到Access Denied.在我的catch块中,我想确保引发的异常确实是Access Denied.有没有办法可以得到它的错误代码?Acceess Denied的Win32错误代码是5.我不想在错误消息中搜索拒绝字符串或类似的东西.
谢谢