当另一个Windows服务重新启动时,Windows服务如何以编程方式自动重启?
问题描述:
我对Windows服务项目工作,我要重新启动我的Windows服务时Lync Front-End service
重新启动当另一个Windows服务重新启动时,Windows服务如何以编程方式自动重启?
我知道我如何重新启动我的Windows服务,我用这个answer,但我不知道我怎么重新启动它时前-End服务已重新启动
,我知道我可以使用ServiceController
类
答
运行这段代码在服务检查Windows服务的状态:
EventLog log = EventLog.GetEventLogs().First(o => o.Log == "Security");
log.EnableRaisingEvents = true;
log.EntryWritten += (s, e) => {p.print(e); };
写下事件的方法登录
void test(EntryWrittenEventArgs entry)
{
//you can check event log in the log viewer and set
//EventLogEntryType and InstanceId accordingly.
EventLogEntry evntLog=entry.Entry;
if (evntLog.EntryType == EventLogEntryType.SuccessAudit &&
evntLog.InstanceId==123)
{
//Code to restart the service
}
}
答
I)Topshelf是容易开发Windows服务的一个开源项目。它会节省你的时间,即使你决定不使用它,你可以从它的源代码中学习。
II)简短的回答是,你必须轮询其他服务的状态。没有(普通的)基于事件的机制。
如果你不能让其他服务的任何修改,那么你就可以查询了它的地位(在一个单独的任务):
var sc = new ServiceController(serviceName);
status = sc.Status;
并使用你所提到的答案,重新启动你的(和再次在OnStart中轮询,直到其他服务完全开始)。
但是,如果您可以修改其他服务,那么您可以使用其他方式(如管道或命名互斥体)来执行此操作(同样需要在单独的任务中或在OnStart上或在OnStop上进行轮询)。
您是否在重新启动'Lync前端服务'后收到任何事件日志? – Arshad
重新启动它有什么好处?你不能只是再次打电话给OnStart吗? – nvoigt
@nvoigt是的,我可以..但仍然存在问题 – tito11