Stopping threads without Thread.Abort()
-
I'm pretty new to multithreaded code. I understand a lot of the concepts (or think I do), but I'm extremely green in applying them. That being said, I have a webservice that is starting three threads. These threads could finish anywhere from a few minutes to a day depending on how much data is being processed from an external DB. The situation I'm currently experiencing is that if one thread fails, I'd like to stop the other two threads in order to save resources. Initially I was using Thread.Abort(), but I quickly realized that when catching the ThreadAbortException and aborting the other threads, that would spawn additional exceptions, which would require a new try/catch block, etc. I started googling and saw more...impolite :) things said about Thread.Abort() and how it's hard to determine when the exception will even be thrown. So, I'm trying to find a way to stop my threads without using Thread.Abort. I've seen people mention using a flag or Mutex to signal the threads to stop, but naturally that means all my methods need to check for this flag in many places, and I don't see a clean way to do that. Most examples I see will do something like: (while !stopThread) { // do some work } which is very convenient of them. How is this relevant when the work is a one-time thing, not something you'd loop through? Is there something I'm missing? Thanks for any and all help. :cool:
-
I'm pretty new to multithreaded code. I understand a lot of the concepts (or think I do), but I'm extremely green in applying them. That being said, I have a webservice that is starting three threads. These threads could finish anywhere from a few minutes to a day depending on how much data is being processed from an external DB. The situation I'm currently experiencing is that if one thread fails, I'd like to stop the other two threads in order to save resources. Initially I was using Thread.Abort(), but I quickly realized that when catching the ThreadAbortException and aborting the other threads, that would spawn additional exceptions, which would require a new try/catch block, etc. I started googling and saw more...impolite :) things said about Thread.Abort() and how it's hard to determine when the exception will even be thrown. So, I'm trying to find a way to stop my threads without using Thread.Abort. I've seen people mention using a flag or Mutex to signal the threads to stop, but naturally that means all my methods need to check for this flag in many places, and I don't see a clean way to do that. Most examples I see will do something like: (while !stopThread) { // do some work } which is very convenient of them. How is this relevant when the work is a one-time thing, not something you'd loop through? Is there something I'm missing? Thanks for any and all help. :cool:
Shawn H wrote:
How is this relevant when the work is a one-time thing, not something you'd loop through? Is there something I'm missing?
Hmm, so there's no loop in your threads? Each thread just has a sequence of steps that is performed? Could you choose some strategic places within those steps to check to see if all is well?
if(everythingOk)
{
DoSomething();
}if(everythingOk)
{
DoSomethingElse();
}// etc...
Kind of cumbersome, but it's the only way I can think of offhand for terminating a loopless thread "naturally."