这就是我得到的:
protected override void OnStart(string[] args)
{
if (SomeApp.Initialize())
{
SomeApp.StartMonitorAndWork();
base.OnStart(args);
}
}
protected override void OnStop()
{
SomeApp.TearDown();
base.OnStop();
}
Run Code Online (Sandbox Code Playgroud)
这里Initialize读取一个配置文件,如果有错,那就没什么可做的,所以服务应该停止!如果配置正常StartMonitorAndWork启动:
Timer(new TimerCallback(DoWork), null, startTime, loopTime);
Run Code Online (Sandbox Code Playgroud)
和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?
或者应该有这样的事情: …