When to call base.Onstart
-
I have a class that inherits System.ServiceProcess.ServiceBase and is overriding the OnStart (string[] args) method. Where do I call base.OnStart(args)? At the beginning of the method or at the end?
protected override void OnStart(string[] args)
{
// Do Stuff
base.OnStart(args);
}Or:
protected override void OnStart(string[] args)
{
base.OnStart(args);
// Do Stuff
} -
I have a class that inherits System.ServiceProcess.ServiceBase and is overriding the OnStart (string[] args) method. Where do I call base.OnStart(args)? At the beginning of the method or at the end?
protected override void OnStart(string[] args)
{
// Do Stuff
base.OnStart(args);
}Or:
protected override void OnStart(string[] args)
{
base.OnStart(args);
// Do Stuff
}Normally
OnXxx
methods are used for raising events (and occaisionaly other logic that relates to an action but that should be documented). If your code in _Do stuff_relies upon this event already being raised (or any other logic that might be in the base method being applied) then you should call it first. If it's important that your Do stuff is done before any events are raised etc then you should call it afterwards. If it doesn't matter then it doesn't matter!Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Normally
OnXxx
methods are used for raising events (and occaisionaly other logic that relates to an action but that should be documented). If your code in _Do stuff_relies upon this event already being raised (or any other logic that might be in the base method being applied) then you should call it first. If it's important that your Do stuff is done before any events are raised etc then you should call it afterwards. If it doesn't matter then it doesn't matter!Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)DaveyM69 wrote:
If it doesn't matter then it doesn't matter!
when you're right, you're right indeed! :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
DaveyM69 wrote:
If it doesn't matter then it doesn't matter!
when you're right, you're right indeed! :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
:laugh:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I have a class that inherits System.ServiceProcess.ServiceBase and is overriding the OnStart (string[] args) method. Where do I call base.OnStart(args)? At the beginning of the method or at the end?
protected override void OnStart(string[] args)
{
// Do Stuff
base.OnStart(args);
}Or:
protected override void OnStart(string[] args)
{
base.OnStart(args);
// Do Stuff
}That depends on what else you do in your OnStart; I call
base.OnStart
in the middle:protected override void OnStart ( string\[\] args ) {
if DEBUG
System.Threading.Thread.Sleep ( 60000 ) ;
endif
this.oktoproceed = true ; base.OnStart ( args ) ; if ( this.when != null ) { this.when.Start() ; } LogMessage ( this.ServiceName + " started" ) ; return ; }
(The Sleep gives me time to attach the debugger.
when
is aSystem.Timers.Timer
.) -
Normally
OnXxx
methods are used for raising events (and occaisionaly other logic that relates to an action but that should be documented). If your code in _Do stuff_relies upon this event already being raised (or any other logic that might be in the base method being applied) then you should call it first. If it's important that your Do stuff is done before any events are raised etc then you should call it afterwards. If it doesn't matter then it doesn't matter!Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)Sorry for not getting back sooner. In the OnStart method I'll be creating a couple of threads, a dozen or sockets and creating some objects but they won't depend on base.OnStart(). Let's say that something in my OnStart fails (file not found for example), how would I report that startup has failed and stop the startup of the service? Do I simply not call base.OnStart()?
modified on Sunday, December 6, 2009 9:15 PM