Hello everyone,
To implement a service, I think we just need to derived from ServiceBase class, and implement some event handler function, like OnStart, OnStop, etc.
As described in the sample,
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase(VS.80).aspx
What is the function of method InitializeComponent? I found it never called explicitly in the class SimpleService, nor there are any document from MSDN about this method. Any ideas?
thanks in advance,
George
George GeorgePosted Apr 14, 2008, 10:02 AM
Cool Alan, question answered!
regards,
George
AlanPosted Apr 14, 2008, 9:56 AM
That's right, George.
My personal view is that, if you're going to use the VS template to create a windows service, then you should always call InitializeComponent from the constructor so that you don't get out of step with the designer.
However, if you're not using the template, then you can do whatever seems appropriate in the circumstances.
George GeorgePosted Apr 14, 2008, 8:20 AM
Thanks Alan,
Now I understand this method should be called by us explicitly if we want to use it (e.g. in constructor), right?
Previously, I have the confusion that this method will be called automatically by some other class (e.g. some other .NET SDK class).
regards,
George
AlanPosted Apr 14, 2008, 6:40 AM
When you create a Windows service project in Visual Studio using the supplied template and set some properties in the Properties Box, the designer creates an InitializeComponent() method containing the property settings and then inserts a call to it in the service class's constructor. This is analogous to the situation in a windows forms application.
See, for example, this article by Mahesh:
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx
The example in the MSDN docs is a bit of a muddle in that the InitializeComponent() method has been created with the property settings for the service but then it isn't actually being called from the constructor - the property settings (or three of them) are duplicated in the constructor instead!
So, generally, it keeps things tidy if you call InitializeComponent() from the constructor rather than set the properties in the constructor and you certainly don't want to duplicate the two.