473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Another Service/Thread question.

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
3 1615

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randy Geyer | last post by:
I have an NT/W2K service that spawns 3 worker threads that each do different things. For example, one monitors an email mailbox for incoming items and puts them in a queue for processing by a 2nd...
5
by: Eric Chong | last post by:
I created a Windows Service in C# that requires to get passed command arguments like a Console App. I noticed that there is an option "Start parameters" text box in the property of a Windows...
13
by: BK | last post by:
Can someone point me to a code sample that illustrates executing long running tasks (asynchronous) from a web application in ASP.NET? I assume that Web Services might come into play at some point,...
2
by: Drew Stoddard | last post by:
We are working in C#/Winforms and are using asynchronous client-side web service calls (the Begin... and End... methods supplied by the web reference creation). Many of these calls are contained...
0
by: Stephen Barrett | last post by:
After reading through the many many many posts on threading, I still don't quite understand how to accomplish a task. Here is an overview of the app I am coding. I am sorry if it is a little...
8
by: Jerry Camel | last post by:
I want my service to terminate automatically if the specified parameters are invalid. I tried to use a servicecontroller component to attach to the service, but I think that it's failing because...
17
by: UJ | last post by:
Is there any way for a windows service to start a windows program ? I have a service that will need to restart a windows app if it needs to. TIA - Jeff.
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
2
by: tshad | last post by:
I have a Service that starts a thread that never ends. When I stop the service from the Service Applet, does it kill the thread or do I need to do it myself? If it isn't killed, what happens...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.