Andy
Andy

Reputation: 11512

handling stop in a .net windows service

I'm sure this is a really common case but I can't find much on MSDN about it.

let's say I have a service which is performing transactions and when I get a stop request I want to make sure I have completed the current transaction before the actual process is terminated.

I know that windows seems to have a "stopping" state but I'm not sure how to control that from the ServiceBase.OnStop method.

Am I expected to return from OnStop as soon as I have registered the request to stop? IF so, how do I indicate when I am actually finished?

Or alternatively should I block and not return from OnStop until the current transaction has completed? (i.e. windows automatically puts the service in the "stopping" state between the call to OnStop and when OnStop returns)

Upvotes: 3

Views: 2674

Answers (1)

nvoigt
nvoigt

Reputation: 77374

If you have some kind of thread doing the transaction, you should wait for it to finish (or be cancelled, your decision) in OnStop. If OnStop returns, the service should be stopped, not just flagged for stopping or something similar.

Upvotes: 2

Related Questions