Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stopping threads without Thread.Abort()

Stopping threads without Thread.Abort()

Scheduled Pinned Locked Moved C#
databasehelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JHubSharp
    wrote on last edited by
    #1

    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:

    L 1 Reply Last reply
    0
    • J JHubSharp

      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:

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups