Re: Thread: Thread.Abort() and SmtpMail does not work thougether? by Willy
Willy
Fri Jan 13 09:23:03 CST 2006
Don't sleep for that long, sleep repeadedly for short periods and check for
an event or a shared flag, but don't use Thread.Abort.
Here is a sample...
//calling thread (or OnStop).
// shared variable
bool continue= true;
...
// order service thread to stop
continue = false;
// your service thread
...
while(true){
// Sleep at most 10 minutes or until continue == false
// check the continue flag every 5 seconds
while((continue) || cnt < 120)
Thread.Sleep(5000);
if(!continue)
return; // exit from thread procedure
else
// do what you have to do
...
}
Willy.
"Willy S" <WillyS@discussions.microsoft.com> wrote in message
news:ED2762C6-CDA5-4135-AA9D-068A5C46F509@microsoft.com...
|
| DAMN!!
|
| I had a long answer that got lost when posting - because the service
wonted
| me to log in again!
| But for reading the posts I dont have to log in again :-(
|
|
| So for short:
|
| I am using OnStop when running as a service and a stop button when running
| as a application (for test).
|
| I wont the service to wait until the child thread is in sleep mode and not
| running before exiting.
| And it must be possible to stop it while in sleep mode (that can be
several
| minutes).
|
| The same goes when running as an application.
|
| - WS
|
|
| >
| > You are missing the point of windows services here. Services are meant
to
| > run in the background and stay running until system shutdown or until
they
| > are requested to stop (which is exactly what happens at system shudown),
| > that means you should "stop" the service using the SCM provided protocol
| > (stop, pause, start...), and handle the stop event in your OnStop
| > eventhandler.
| > Using Thread.Abort does not stop the application (unless it's the main
| > thread), it 'tries' to abort a thread, if that thread happens to be
blocked
| > in unmanaged code, the Abort will not be honoured. If you need to stop
an
| > application cold, you'll have to call Environment.Exit, but again
Services
| > aren't regular applications, use the protocols provided to control them.
| >
|