Hello everyone,
Suppose we have a class which is derived from ServiceBase, which provides Windows Service. My question is, what is the best practices that what kind of code to put into constructor of the class, and what kind of code to put into OnStart method of the class?
thanks in advance,
George
George GeorgePosted Apr 15, 2008, 4:06 AM
Thanks Alan,
Question answered.
regards,
George
AlanPosted Apr 14, 2008, 10:52 AM
I don't know whether there's anything written down by MS about this but my own view is that:
1. You should only put initialization code for the service in the constructor, namely the call to InitializeComponent() if you're using VS followed by any other initialization which can't be done simply by setting property values in the Properties Box. Remember that the constructor will only be called once even if the service is stopped and then restarted again, so you only want stuff in here which isn't going to change.
2. You should only put code required to start the service in the On Start() method. This includes code to create resources needed by the service and code to create and launch a new thread or threads. Remember that, if the service may be stopped and restarted, then you may need to release those resources in the OnStop() method and recreate them again in OnStart().