0

I have a parent service that watches other child service processes. When I need to kill a certain child process via the process' object.Kill() and waiting for it to complete. Then, I'm surprised that this is not logged to the Event Viewer Application log with the process' name as the instance name, and the message as "Service stopped successfully"? When the parent service start up the child service process again, I do see an entry in the event viewer's application log "Service started successfully".

Any ideas?

Thanks!

sOltan
  • 447
  • 1
  • 7
  • 20

3 Answers3

3

Applications have to log things in the application log themselves -- using Process.Kill is not a graceful way of asking a service to stop, so it won't log it's stopping (as it's been kicked out of memory)

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

Instead of using Process.Kill() to control your service try using the ServiceController class. For an example see similar question here. For writing to the EventLog see the documentation for the EventLog and the WriteEntry method

Community
  • 1
  • 1
Dan Busha
  • 3,723
  • 28
  • 36
1

Yes, you should be using the ServiceController class to stop or pause services rather than killing them.

You will need to add the using for the System.ServiceProcess namespace.

If your looking to stop dependencies (children), you can use the code below to grab the dependent service of what ever it is your looking for and stop the appropriate child services.

ServiceController sc = new ServiceController("%PARENT_SERVICE%");
ServiceController[] scServices = sc.DependentServices;

foreach (ServiceController dependant in scServices)
{
    if (dependant.DisplayName == "%CHILD_SERVICE%")
    {
        dependant.Stop();
        dependant.WaitForStatus(ServiceControllerStatus.Stopped);
    }
}
riQQ
  • 9,878
  • 7
  • 49
  • 66
Derek
  • 8,300
  • 12
  • 56
  • 88