mary jean ligas

mary jean ligas

  • NA
  • 420
  • 195.9k

windows service application can detect its own status?

Jun 19 2017 5:55 AM
is there a way that a windows service application can detect its own status upon restarting the service itself?
i want to handle that if a user restart the service and the status is still start pending because the service finish the execution of methods then it wont allow to restart it. How can i do that? I do some code on the OnStart part but the error shows that "The service on local computer started and then stopped. Some services stop automatically if they are not in the use by other services of programs."

here's my snippet:
protected override void OnStart(string[] args)
{
using (ServiceController sc = new ServiceController(ServiceName))
{
if(sc.Status == ServiceControllerStatus.StartPending)
{
//will not allow to restart the service
}
else
{
//call methods
}
}
}

Answers (1)