Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Need best way to terminate multiple threads immediately (Suspend is now obsolete)

Posted on 2007-08-10
Medium Priority
Last Modified: 2013-12-17
I have a class which starts up a timer.  Each time the timer goes off, the program grabs a bunch of records from a database.  It splits those records up among five arrays, and spawns off five threads to process each array.

The class has a shutdown method which suspends and destroys the timer, then goes through and does a .Suspend on each thread which is running.

.NET is telling me that .Suspend is obsolete, which I assume means I'm not supposed to use it, but then all it tells me is that I'm supposed to use Monitor, Mute, Event or Semaphore instead, but it doesn't give me any idea of how to do that.

All I want to do is loop through the array of pointers to the threads and shut each of them down.  Can anybody tell me a quick and clean way to do that?
Question by:FrancineTaylor
  • 3
  • 2
LVL 37

Expert Comment

ID: 19673013
Thread.Abort ...

just keep references to your threads
LVL 37

Accepted Solution

gregoryyoung earned 1000 total points
ID: 19673021
Thread.Abort is evil and can have all sorts of other issues caused by it .... your alternative would be to use a stop variable that you check in your thread and then Join() to your thread.

Author Comment

ID: 19673179
Okay, thanks, I tried just using .Abort() and I got all kinds of evil exceptions from inside my methods on all my threads.  I like the second solution better.  Here's the code that seems to work:

bool destroyed = false;

public void ShutMeDown() {
    lock (lockThreadObject) {
        destroyed = true;
        // turn off the timer
        if (timerSafe != null) {
            timerSafe = null;
        // terminate each of the threads
        for (int curThread = 0; curThread < maxThreadsToUse; curThread++) {
            if (processThread[curThread] != null) {

                // give the thread 3 seconds to terminate.  Each call to UpdateRecord
                //   takes about a second to process, so this gives plenty of leeway
                processThread[curThread].Join(new TimeSpan(0, 0, 0, 3, 0));

                //processThread[curThread].Suspend();     // obsolete ???
                processThread[curThread] = null;


// This is the method called by each thread
public void UpdateDialerSubset(object pParm) {
    if (pParm is MyRecord[]) {
        MyRecord[] records = pParm;

        int numToProcess = records.Length;       // debug
        for (int ctr = 0; ctr < numToProcess; ctr++) {
            // if we are shutting down, stop here
            if (destroyed) {
LVL 37

Expert Comment

ID: 19673363
Yes Thread.Abort throws a ThreadAbortException on the thread.

Author Comment

ID: 19673374
Post archive note: I just realized that locking down in my shutdown method is a bad idea.  It locks up the timer object so that the threads can get hung up waiting on ShutMeDown to take off the lock, and ShutMeDown is waiting for the threads to finish.  Oops.

So just delete the "lock (lockThreadObject) {" line from my code...

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question