Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3634300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:45:42+00:00 2026-05-19T00:45:42+00:00

edit: this seems to be a sticking point, so I’ll just get it out

  • 0

edit: this seems to be a sticking point, so I’ll just get it out of the way:

I’m not using this for synchronization. Simply to kill long-running tasks when they no longer become necessary / desirable.
Hypothetically: assume these threads simply write to the console (say: “Thread 1”), sleep a random length, and then exit. If they’re aborted, I want them to inform me with another console write (say: “Thread 1 aborted”). And I wish to be able to jump straight to the abort code if I try to abort it prior to it being run, without any chance of it performing its normal functions. If it’s aborted during normal functioning, it will of course print both.


I have a chunk of threads I wish to run in order, on an ASP site running .NET 2.0 with Visual Studio 2008 (no idea how much all that matters, but there it is), and they may have aborted-clean-up code which should be run regardless of how far through their task they are. So I make a thread like this:

Thread t = new Thread(delegate() {
   try { 
      /* do things */ 
      System.Diagnostics.Debug.WriteLine("try");
   }
   catch (ThreadAbortException) {
      /* cleanup */ 
      System.Diagnostics.Debug.WriteLine("catch");
   }
});

Now, if I wish to abort the set of threads part way through, the cleanup may still be desirable later on down the line. Looking through MSDN implies you can .Abort() a thread that has not started, and then .Start() it, at which point it will receive the exception and perform normally. Or you can .Join() the aborted thread to wait for it to finish aborting. Presumably you can combine them.

http://msdn.microsoft.com/en-us/library/ty8d3wta(v=VS.80).aspx
To wait until a thread has aborted, you can call the Join method on the thread after calling the Abort method, but there is no guarantee the wait will end.
If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

Now, when I debug and step through this code:

t.Abort(); // ThreadState == Unstarted | AbortRequested
t.Start(); // throws ThreadStartException: "Thread failed to start."
// so I comment it out, and
t.Join(); // throws ThreadStateException: "Thread has not been started."

At no point do I see any output, nor do any breakpoints on either the try or catch block get reached.
Oddly, ThreadStartException is not listed as a possible throw of .Start(), from here: http://msdn.microsoft.com/en-us/library/a9fyxz7d(v=VS.80).aspx (or any other version)

I understand this could be avoided by having a start parameter, which states if the thread should jump to cleanup code, and foregoing the Abort call (which is probably what I’ll do). And I could .Start() the thread, and then .Abort() it. But as an indeterminate amount of time may pass between .Start and .Abort, I’m considering it unreliable, and the documentation seems to say my original method should work.

Am I missing something? Is the documentation wrong?

edit: ow. And you can’t call .Start(param) on a non-parameterized Thread(Start). Is there a way to find out if a thread is parameterized or not, aside from trial and error? I see a private m_Delegate, but nothing public…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:45:43+00:00Added an answer on May 19, 2026 at 12:45 am

    When using …

    t.Abort();
    t.Start();
    

    … the innerException of the ThreadStartExcpetion will contain the ThreadAbortExeption just as msdn states: (if Abort is called on a thread that has not been started, the thread will abort when Start is called)

    If you execute …

    t.Start();
    t.Abort();
    

    … the ThreadAbortException may never occure because the thread had no time to start before it was aborted.

    If you test something like …

    t.Start();
    Thread.Sleep(100);
    t.Abort();
    

    … the “cleanup” code should always be executed.

    If you need to execute cleanup-code even if the Thread has not started to do anything a possible solution would to start a cleanup method together with the Abort call.

    Moreover you should try to avoid aborting threads and use different techniques (http://msdn.microsoft.com/en-us/library/ms228964.aspx)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like such a simple question. I have several Edit boxes on my
EDIT: This seems to be a problem with IE exclusively. Very simple to re-create,
Edit For VB 6 End Edit Hey this seems like it should be an
EDIT: This seems to be happening on all pages on my application with any
This seems so simple, but I can't figure it out... I would like all
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
Edit This question has gone through a few iterations by now, so feel free
EDIT: This question is a duplicate of What is the difference between managed and
EDIT: This issue is already submitted on Github. Changing the Act part to this
[Edit: This problem applies only to 32-bit systems. If your computer, your OS and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.