I’m trying to understand the difference between OnStart() and the constructor in a ServiceBase derived class. From reading around it seems that the first time you start a service (after turning on your machine), the constructor is called. Thereafter, you can stop and start the service as many times as you like, but the constructor will never be called again, only the OnStart() method will be called each time. Can anyone confirm?
Thanks
Do not use the constructor to perform processing that should be in
OnStart. UseOnStartto handle all initialization of your service. The constructor is called when the application’s executable runs, not when the service runs. The executable runs beforeOnStart. When you continue, for example, the constructor is not called again because theSCMalready holds the object in memory. IfOnStopreleases resources allocated in the constructor rather than inOnStart, the needed resources would not be created again the second time the service is called.MSDN