Stop Windows Service

  • Thread starter Thread starter Peter Larsen [CPH]
  • Start date Start date
P

Peter Larsen [CPH]

Hi,

What is the normal way to stop a servie from within the servie itself ??

BR
Peter
 
What is the normal way to stop a servie from within the servie itself ??

The service extends ServiceBase and that has a Stop method.

I have never tried it, but I would expect it to
stop the service.

Arne
 
Thanks for your comments.

I know about ServiceBase.Stop(), but what is best practice ?

What is if i want to stop with error (Environment.Exit() or throw an
exception) ?
Both ways, the service stops immediately without any cleanups (no go i would
say).

/Peter
 
Thanks for your comments.

I know about ServiceBase.Stop(), but what is best practice ?

What is if i want to stop with error (Environment.Exit() or throw an
exception) ?
Both ways, the service stops immediately without any cleanups (no go i
would say).

So write a cleanup method whose final line calls Stop(). Stopping with an
error code is a bad idea in my opinion, like some throwback to DOS. I
recommend writing an error to the event log instead of an returning error
code.
 
Hello Peter,

If you want to log the error in service exiting, you can consider calling
EventLog.WriteEntry to write an event log to the system logs.

Check this KB aritcles, http://support.microsoft.com/kb/307024 and the
codes are applicable to the Windows Service application.

By the way, the managed priority subscriber support migrates from the C#
newsgroup to C# forum. So if you have further questions about programming
via C#, we suggest you raise them in the forum,
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads

And you can get more information about the priority subscriber support
migration from this page,
http://msdn.microsoft.com/en-us/subscriptions/aa974230.aspx#CSHARP


Best regards,
Ji Zhou - MSFT
 
Back
Top