By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

Another Service/Thread question.

P: n/a
I'm trying to shutdown my service by stopping all the started classes &
threads.

In each Class I have a Public Sub like this:
Public Sub StopIntf()
Try
Threading.Thread.CurrentThread.Abort()
Catch
End Try
End Sub

In my OnStop routine, I have this:

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
EmailProcessor.StopEmailProcessor()
SmtpSvr.StopServer()
NntpClt.StopNntpClient()
BbsIntf.StopIntf()
End Sub

But when I try to stop my service, I get a message that says the service did
not stop and did not return an error. I look in the Event Log and I get
this:

Description:
Failed to stop service. System.Threading.ThreadAbortException: Thread was
being aborted.
at BoycoTMailNews.BoycoTMailNews.OnStop()
at System.ServiceProcess.ServiceBase.DeferredStop()

How to you properly stop a service that has spawned several threads?
Dec 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Terry Olsen" <to******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm trying to shutdown my service by stopping all the started classes &
threads.

In each Class I have a Public Sub like this:
Public Sub StopIntf()
Try
Threading.Thread.CurrentThread.Abort()
Catch
End Try
End Sub

In my OnStop routine, I have this:

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
EmailProcessor.StopEmailProcessor()
SmtpSvr.StopServer()
NntpClt.StopNntpClient()
BbsIntf.StopIntf()
End Sub


That will abort the thread running OnStop(), not any other thread. To stop
the other threads you should set a global variable and signal them to stop.
Then use Thread.Join() to wait for them to finish. Only after a reasonable
amount of time should you Abort the other threads.

To Abort the other threads you need to keep a reference to each Thread you
create and call Abort on that reference, not on CurrentThread.

David
Dec 12 '05 #2

P: n/a
So would I have a timer in each thread to periodically check the global
variable?

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...

"Terry Olsen" <to******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm trying to shutdown my service by stopping all the started classes &
threads.

In each Class I have a Public Sub like this:
Public Sub StopIntf()
Try
Threading.Thread.CurrentThread.Abort()
Catch
End Try
End Sub

In my OnStop routine, I have this:

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
EmailProcessor.StopEmailProcessor()
SmtpSvr.StopServer()
NntpClt.StopNntpClient()
BbsIntf.StopIntf()
End Sub


That will abort the thread running OnStop(), not any other thread. To
stop the other threads you should set a global variable and signal them to
stop. Then use Thread.Join() to wait for them to finish. Only after a
reasonable amount of time should you Abort the other threads.

To Abort the other threads you need to keep a reference to each Thread you
create and call Abort on that reference, not on CurrentThread.

David

Dec 12 '05 #3

P: n/a

"Terry Olsen" <to******@hotmail.com> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
So would I have a timer in each thread to periodically check the global
variable?


It depends on what the threads are doing. Typically each thread has a main
loop, and the check would go there.

David
Dec 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.