You asked a similar question about stopping a service from OnStart here - https://learn.microsoft.com/en-us/answers/questions/1058630/custom-service-stop
In that thread I commented "Checking for resource availability in OnStart, allowing the service to start, and then spinning up a thread to call Stop() (instead of a thread to do the service's tasks) worked to stop the service gracefully without generating an error."
You can use the same method to solve this problem.