1

I work on windows service project , i want to restart my windows service when Lync Front-End service was restarted

i know how i restart my windows service i use this answer , but i don't know how i restart it when the Front-End Service was restarted

and i know i can check the windows service status by use ServiceController Class

Community
  • 1
  • 1
Tarek Saied
  • 6,482
  • 20
  • 67
  • 111

2 Answers2

0

Run this code in your service :

  EventLog log = EventLog.GetEventLogs().First(o => o.Log == "Security");
  log.EnableRaisingEvents = true;          
  log.EntryWritten += (s, e) => {p.print(e); };

Write a method for event log

  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
            }
       }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
0

I) Topshelf is an open source project for developing Windows Services easily. It will save you time and even if you decide not to use it, you can learn from it's source code.

II) The short answer is you have to poll the status of the other service. There is no (plain) event based mechanism for this.

If you can not make any modifications to the other service then you can poll over it's status by (in a separate Task):

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

And use the answer you've mentioned to restart yours (and again poll in OnStart until the other service get started completely).

But if you can modify the other service then you can use other means like pipes or named mutexes to do that (again needs polling in a separate Task or at OnStart and probably at OnStop).

Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139