Another Service/Thread question.

T

Terry Olsen

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?
 
D

David Browne

Terry Olsen said:
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
 
T

Terry Olsen

So would I have a timer in each thread to periodically check the global
variable?
 
D

David Browne

Terry Olsen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top