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 8763001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:40:50+00:00 2026-06-13T15:40:50+00:00

First of all, Thread.Abort() is evil – got it! Now let me explain my

  • 0

First of all, Thread.Abort() is evil – got it! Now let me explain my situation…

Situation:

I have a dashboard that only displays current running status to the user. It executes various select queries against a Sql Server DB and performs some calculations that are eventually displayed to the user on the dashboard. The user can open multiple dashboards at the same time. I have a timer thread that refreshes the dashboard every few seconds. That timer thread spawns another thread (actually queues work on a threadpool) to perform long running calculations/queries.

The Problem:

When the user clicks the X to close the Dashboard window, it needs to shut down right away (say within a second, I think 2 seconds is too long). So I use this code:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        _timerThread.Abort();
    }

    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        // we're on the timer thread now

        try
        {
            RefreshUi();
        }
        catch (ThreadAbortException)
        {
            System.Threading.Thread.ResetAbort();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

The problem is sometimes the ThreadAbortException is caught, and sometimes it isn’t. I find it’s mostly uncaught when in the middle of executing queries.

[UPDATE] I’ve recently discovered I can catch the ThreadAbortException if I check the innerexception.

Questions:

  1. Is it ok use Thread.Abort() in this case? If so, how do I ALWAYS catch the ThreadAbortException so the user doesn’t see it when closing the Form? (from what I’ve read I don’t think this is possible)
  2. If it’s not ok to use Thread.Abort() (which is what I think most of you will answer), then here are my other concerns with using a “global boolean flag” to tell the thread(s) when to shutdown.
    • I’m concerned the threads won’t stop themselves within the 1 second time limit. (For example, I may have some queries that take 1-2 seconds to run)
    • I will have to clutter my code with: if(shouldShutdown) statements.
    • Should I make the shouldShutdown flag static so all my classes can use it? Will this pose a problem if the user can open multiple dashboards at the same time? (I’m concerned about the static variable being shared across all dashboards. This I definetely don’t want.)
    • When the worker threads see they have to shutdown, should they throw an exception or just return? If they just return, then I’ll have to add even more if(shouldShutdown) checks because it’s ok for some functions to return false. If throwing an exception is ok, which exception do you recommend? A custom one?

Thanks

  • 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-06-13T15:40:51+00:00Added an answer on June 13, 2026 at 3:40 pm

    Right way to do this will be

    1. Making all requests to SQL server asynchronous
    2. Having ManualResetEvent handled in background threads and fired from main thread when it’s time to close.
    3. Interrupting sql requests in AutoResetEvent handlers and gracefully exiting threads
    4. Making GUI close windows in parallel with exiting the threads, so they not depend of each other.
    5. If closing your dashboard means exiting process it will be good to wait all the threads to exit while having windows closed.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, let me say that I am not a professional coder -
I know that this is not the first thread on this topic,but I have
I have a thread that is very simple. The only thing the Thread object
First of all, apologize because I have seen some posts about this, but I
First of all, sorry about the title -- I couldn't figure out one that
First of all, sorry for the too long question. I know that there are
I've got a thread that goes out and looks up data on our (old)
first of all, this is the first week that I use swing, then sorry
Note first of all that this question is not tagged winforms or wpf or
MSDN Thread Hi, all. First of all, please excuse any english language mistakes in

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.