question on errror handling in windows service

  • Thread starter Thread starter sidd
  • Start date Start date
S

sidd

Hi all,
how do i exit out(unload) of a windows service in case of an error.
so i have a code block as follows

try
{
}
catch(exception ex)
{
//log to event log
Application.exit();
}
i can do the above in a winforms application but can i do the same in
a windows service?i understand that i can add the required namespace
in service class and call Application.Exit() ...but is this the right
way to exit from a windows service?

2)assuming application.exit is okay to use in windows service...are
there any events which would be called before windows service exits?
in other words will it fire OnStop or any other event?

3)also when you call application.exit (or even when you stop windows
service form service explorer)..
would it just stop the service
OR
does it also unload the windows service class from memory....in other
words what would happen to a static variable declared in side a
windows service class?
would it get reinitialized to original value ..next time the service
is started? i am asking this because i think the constructor of the
class does not fire each time you restart the class which would mean
that class may not be unloaded from memory...so i am not sure how
static would work?
thanks
siddharth
 
sidd said:
i can do the above in a winforms application but can i do the same in
a windows service?i understand that i can add the required namespace
in service class and call Application.Exit() ...but is this the right
way to exit from a windows service?

No!! :-) I can't remember the exact function but I think it's something like
this.stop.
2)assuming application.exit is okay to use in windows service...are
there any events which would be called before windows service exits?
in other words will it fire OnStop or any other event?

You should get the stop event.
3)also when you call application.exit (or even when you stop windows
service form service explorer)..
would it just stop the service

A service is just an exe, when you stop it the exe is closed like any other
application and removed from memory. All variables should be lost and all
classes should terminate. Constructor should fire again when the service is
started.
 
Back
Top