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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:14:27+00:00 2026-05-23T00:14:27+00:00

In a win form application, I have an array of threads which are started

  • 0

In a win form application, I have an array of threads which are started like this:

bool stop = false;
Thread[] threads = new Thread[10];

for (int i = 0; i < threads.Length; i++)
    threads[i] = new Thread(new ThreadStart(Job));

// How to make sure all threads have exited, when the boolean = false

void Job()
{
    while (!stop)
        // Do something
}

Now if user press STOP, the boolean value for stop will set to true, so threads exit the Job method one after another. How can I make sure all threads are exited?

NOTE: I need traditional threading for my case and TaskLibrary doesn’t fit my scenario.

  • 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-23T00:14:28+00:00Added an answer on May 23, 2026 at 12:14 am

    Have you thought about using BackgroundWorkers instead? You said “traditional threads”..I’m not exactly sure what you mean so I don’t know if this is a valid proposal or not, but here it is anyways in case Join() doesn’t solve your problem

    BackgroundWorker[] workers = new BackgroundWorker[10];
    bool allThreadsDone = false;
    
    // initialize BackgroundWorkers
    for (int i = 0; i < 10; i++)
    {
            workers[i] = new BackgroundWorker();
            workers[i].WorkerSupportsCancellation = true;
            workers[i].RunWorkerCompleted += 
                new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            workers[i].DoWork += new DoWorkEventHandler(AlgorithmsUI_DoWork);
            workers[i].RunWorkerAsync();
    }
    
    // thread entry point..DoWork is fired when RunWorkerAsync is called
    void AlgorithmsUI_DoWork(object sender, DoWorkEventArgs e)
    {
          while (!stop)
                // do something        
    }
    
    // this event is fired when the BGW finishes execution
    private void  worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        bool threadsStillRunning = false;
        foreach (BackgroundWorker worker in workers)
        {
            if (worker.IsBusy)
            {
                threadsStillRunning = true;
                break;
            }
        }
        if (!threadsStillRunning)
            allThreadsDone = true;
    } 
    
    protected override OnFormClosing(FormClosingEventArgs e)
    {
             if (!allThreadsDone)
             {
                  e.Cancel = true;
                  MessageaBox.Show("Threads still running!");
             }
    }
    

    This should prevent your form from closing if any threads are still running.

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

Sidebar

Related Questions

I currently have an application that spawns multiple instances of a single win form.
I have a client-server Silverlight application, which is use Socets. I have server appliaction
In my application I have methods which returns a control (checkbox, radiobutton, textbox) but
I have an existing application (C#/Win forms). I need to add functionality to the
I have an application I have already started working with and it seems I
I have a Winform application in which I am checking if a fully qualified
I have a Winform application built with C# and .Net 2.0. I have a
I have my winform application gathering data using databinding. Everything looks fine except that
I have a winform application that uses some referenced web services to get data.
I have a WinForm application built with VS 2008 (C#) and SQL Server Express

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.