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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:00:35+00:00 2026-06-12T15:00:35+00:00

I am working on a .NET Windows Forms application using Visual Studio 2010 . In this

  • 0

I am working on a .NET Windows Forms application using Visual Studio 2010.

In this application, I need four background threads for underlying data transmission. When ALL the four threads finished, another round of four underlying data transmissions will be started using the four threads again.

The form UI needs to be responsive all the time. My problem is: How can I control the running of the four threads? Like: How do I know that all threads are finished? Using a volatile global counter?

  • 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-12T15:00:36+00:00Added an answer on June 12, 2026 at 3:00 pm

    To answer your question about using specifically four threads, below is a quick sketch using BackgroundWorker. The idea here is to set up four tasks, track the number of running tasks and, when they’re all done restart. For a discussion of volatile vs interlocked, see Stack Overflow question Volatile vs. Interlocked vs. lock.

    This will give you what you’re asking for (four threads, responsive UI), but there’s no error handling, and there could be other problems. Is it possible that one BackgroundWorker would hang (maybe your ‘data transmission’ goes haywire) in which case you’ll end up in a bad state?

    public partial class Form1 : Form  {
        private int workersRunning = 0;
        private List<BackgroundWorker> workers = new List<BackgroundWorker>();
    
        public Form1()  {
            InitializeComponent();
            for (int i = 0; i < 4; i++)  {
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(this.worker_DoWork);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.worker_RunWorkerCompleted);
                workers.Add(worker);
            }
        }
    
        private void button1_Click(object sender, EventArgs e) {
            this.StartWork();
        }
    
        private void StartWork() {
            workers.ForEach(worker => worker.RunWorkerAsync());
        }
    
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){
            Interlocked.Decrement(ref workersRunning);
            Console.WriteLine("Worker reported completion from thread id " + e.Result);
            if(this.workersRunning == 0) {
                Console.WriteLine("All workers are done. Start again");
                this.StartWork();
            }  else  {
                Console.WriteLine(this.workersRunning + " workers are still running.");
            }
        }
    
        void worker_DoWork(object sender, DoWorkEventArgs e) {
            Interlocked.Increment(ref workersRunning);
            int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("Doing work on thread #" + threadId);
    
            Thread.Sleep(new Random().Next(2000, 5000));
    
            e.Result = "Work done on thread id " + threadId;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on windows forms application using c#.net. i have developed an application
I'm developing a windows forms application using VB.NET. I'm currently working on DataReport (by
I am working on windows forms application using c#.net. i have developed an application(3-tier)
I am working on windows forms applications using c# .net 4.0. Where I have
I'm working on a .NET Windows Forms application. I have an event handler procedure
I am working on a Windows Forms application in C#/.Net. I want to use
I am working on a mobile application in .NET. This is a windows mobile
When working with an ASP.NET application in Visual Studio 2008, my errors window often
I am working on a .NET 3.5 Windows Forms application that will interact with
I am working with a vb.net Windows Forms application which has Application Settings, running

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.