.net – How to stop a service within the OnStart method


I'm writing a windows service in .net 2.0.

What should I do within the OnStart() method if for some reason I don't want my service to start? Can I simply call the Stop() method of the base class (ServiceBase)? I've tried that and am getting some funny memory access exceptions. Is it required to start a separate thread to call the Stop() method?

Best Solution

Why do you want to do that? If there's an error, log it in event log and throw an exception. Your service won't be started and the user will be informed that something bad happened.