How to shutdown a custom service from OnStart function in c#

Darryl Hoar 21 Reputation points
2023-01-13T20:46:37.15+00:00

I have created a custom service using Visual Studio 2019. In the OnStart function, I call a function that verifies the parameters and values in the config file are correct. If they are not, it throws a custom exception which writes to the eventlog, then the service dies. Windows pops up a message The [custom service name] service on local computer started and then stopped... when this occurs.

Can I prevent the windows pop up? Can I customize its message? Or am I aborting the start using the wrong technique?

help greatly appreciated.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,036 questions
No comments
{count} votes

Accepted answer
  1. RLWA32 28,676 Reputation points
    2023-01-13T23:46:34.0766667+00:00

    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.


0 additional answers

Sort by: Most helpful