1

我在 Windows 服务项目上工作,我想在重新启动时重新启动我的 WindowsLync Front-End service服务

我知道如何重新启动我的 Windows 服务我使用这个答案,但我不知道在重新启动前端服务时如何重新启动它

我知道我可以通过使用ServiceController类检查 Windows 服务状态

4

2 回答 2

0

I) Topshelf是一个用于轻松开发 Windows 服务的开源项目。它将节省您的时间,即使您决定不使用它,您也可以从它的源代码中学习。

II)简短的回答是您必须轮询其他服务的状态。没有(普通)基于事件的机制。

如果您不能对其他服务进行任何修改,那么您可以通过(在单独的任务中)轮询它的状态:

var sc = new ServiceController(serviceName);
status = sc.Status;

并使用您提到的答案重新启动您的答案(并再次在 OnStart 中轮询,直到其他服务完全启动)。

但是,如果您可以修改其他服务,那么您可以使用其他方式(如管道或命名互斥锁)来执行此操作(再次需要在单独的任务中或在 OnStart 并且可能在 OnStop 进行轮询)。

于 2013-05-16T10:15:27.763 回答
0

在您的服务中运行此代码:

  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
            }
       }
于 2013-05-16T08:39:22.617 回答