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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:33:51+00:00 2026-05-16T10:33:51+00:00

I have a singleton that has a running thread for obtaining records from a

  • 0

I have a singleton that has a running thread for obtaining records from a server.
But when I stop my winform application the thread keeps running. I have tried to create a destructor in my singleton to abort the thread if it running, but it does not have any effect on the thread – I know that the destructor is being evoked.

I am looking for suggestions on how I should shut down a thread when my application closes.
thanks

C#, .net2

  • 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-16T10:33:52+00:00Added an answer on May 16, 2026 at 10:33 am

    The best option, if possible in your application, is cooperative cancellation.

    A thread automatically stops when it has no more code to execute. So, when the user closes your application, you set a flag indicating that your thread should stop. The thread needs to check from time to time if the flag is set and, if so, stop obtaining records from the server and return.

    • As @Hans Passant noted, BackgroundWorker has built-in support for this.
    • If you can upgrade, the .NET Framework 4.0 introduces a whole set of new classes that support cooperative cancellation of asynchronous operations.

    Otherwise, you can roll your own solution, for example

    static bool isCancellationRequested = false;
    static object gate = new object();
    
    // request cancellation
    lock (gate)
    {
        isCancellationRequested = true;
    }
    
    // thread
    for (int i = 0; i < 100000; i++)
    {
        // simulating work
        Thread.SpinWait(5000000);
    
        lock (gate)
        {
            if (isCancellationRequested)
            {
                // perform cleanup if necessary
                //...
                // terminate the operation
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java singleton class that has my application settings. I used this
I have a texture manager that I am running as singleton, and wish to
I have an app that has settings the user can pick before running the
I have developed a small iPhone application by using singleton that I use to
I'm looking to have a class (subclassed from threading.Thread) that is initialised and started
I have an existing set of singleton WCF services. They are long-running processes that
I have an application that depends on connection It has many Activities.I need to
I have a singleton class, that has a map which can be accessed by
I'm running a web application on Tomcat. I have a class that handles all
I have a plug-in to an Eclipse RCP application that has a view. After

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.