Design question about windows services...
-
So in
OnStop
I create and start a thread withnew Thread(new ThreadStart(funk))
, right? Thread is doing something and then Stop command comes along and naturaly I getOnStop
called. Everything is OK so far. I post to my thread a message somehow for it to quit andOnStop
returns right away. SCM thinks that everything was ok. But my worker thread has some work to finish before quitting and it may take time. So it continues until it's done BUT magically the service process disappears in about 20 sec afterOnStop
returned cutting the execution of my thread right in the middle (or so it seems) of a routine. X| How do I avoid that? :confused: I want that thread to finish normaly. -
So in
OnStop
I create and start a thread withnew Thread(new ThreadStart(funk))
, right? Thread is doing something and then Stop command comes along and naturaly I getOnStop
called. Everything is OK so far. I post to my thread a message somehow for it to quit andOnStop
returns right away. SCM thinks that everything was ok. But my worker thread has some work to finish before quitting and it may take time. So it continues until it's done BUT magically the service process disappears in about 20 sec afterOnStop
returned cutting the execution of my thread right in the middle (or so it seems) of a routine. X| How do I avoid that? :confused: I want that thread to finish normaly.You need to wait in your OnStop function until your service has finished shutting down. You might do this with a ManualResetEvent like this:
ManualResetEvent done = new ManualResetEvent(false); protected override void OnStop() { // Tell your service to shut down done.WaitOne(); }
Then in your other routine, running on the other thread, just calldone.Set();
when you are all done, just before returning. Burt Harris -
You need to wait in your OnStop function until your service has finished shutting down. You might do this with a ManualResetEvent like this:
ManualResetEvent done = new ManualResetEvent(false); protected override void OnStop() { // Tell your service to shut down done.WaitOne(); }
Then in your other routine, running on the other thread, just calldone.Set();
when you are all done, just before returning. Burt HarrisBut I already tried even a message box in onstop method. That should've keep it alive for as long as there is the message box, right? Wrong! The process magicaly dissapears in 20 seconds after stop button was hit in SCM and it seem it doesn't metter what I do. The execution of a function just gets cut off right in the middle of a routine. Looks like SCM uses "terminate process" or something...