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

Darryl Hoar 121 Reputation points

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.

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.
9,938 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 38,856 Reputation points

    You asked a similar question about stopping a service from OnStart here -

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful