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

The Archive Base Latest Questions

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

I’ve never really used threading before in C# where I need to have two

  • 0

I’ve never really used threading before in C# where I need to have two threads, as well as the main UI thread. Basically, I have the following.

public void StartTheActions()
{
  // Starting thread 1....
  Thread t1 = new Thread(new ThreadStart(action1));
  t1.Start();

  // Now, I want for the main thread (which is calling `StartTheActions` method)
  // to wait for `t1` to finish. I've created an event in `action1` for this.
  // The I wish `t2` to start...

  Thread t2 = new Thread(new ThreadStart(action2));
  t2.Start();
}

So, essentially, how can I have a thread wait for another one to finish? What is the best way to do this?

  • 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-13T16:23:27+00:00Added an answer on May 13, 2026 at 4:23 pm

    I can see five options available:

    1. Thread.Join

    As with Mitch’s answer. But this will block your UI thread, however you get a Timeout built in for you.


    2. Use a WaitHandle

    ManualResetEvent is a WaitHandle as jrista suggested.

    One thing to note is if you want to wait for multiple threads: WaitHandle.WaitAll() won’t work by default, as it needs an MTA thread. You can get around this by marking your Main() method with MTAThread – however this blocks your message pump and isn’t recommended from what I’ve read.


    3. Fire an event

    See this page by Jon Skeet about events and multi-threading. It’s possible that an event can become unsubcribed between the if and the EventName(this,EventArgs.Empty) – it’s happened to me before.

    (Hopefully these compile, I haven’t tried)

    public class Form1 : Form
    {
        int _count;
    
        void ButtonClick(object sender, EventArgs e)
        {
            ThreadWorker worker = new ThreadWorker();
            worker.ThreadDone += HandleThreadDone;
    
            Thread thread1 = new Thread(worker.Run);
            thread1.Start();
    
            _count = 1;
        }
    
        void HandleThreadDone(object sender, EventArgs e)
        {
            // You should get the idea this is just an example
            if (_count == 1)
            {
                ThreadWorker worker = new ThreadWorker();
                worker.ThreadDone += HandleThreadDone;
    
                Thread thread2 = new Thread(worker.Run);
                thread2.Start();
    
                _count++;
            }
        }
    
        class ThreadWorker
        {
            public event EventHandler ThreadDone;
    
            public void Run()
            {
                // Do a task
    
                if (ThreadDone != null)
                    ThreadDone(this, EventArgs.Empty);
            }
        }
    }
    

    4. Use a delegate

    public class Form1 : Form
    {
        int _count;
    
        void ButtonClick(object sender, EventArgs e)
        {
            ThreadWorker worker = new ThreadWorker();
    
            Thread thread1 = new Thread(worker.Run);
            thread1.Start(HandleThreadDone);
    
            _count = 1;
        }
    
        void HandleThreadDone()
        {
            // As before - just a simple example
            if (_count == 1)
            {
                ThreadWorker worker = new ThreadWorker();
    
                Thread thread2 = new Thread(worker.Run);
                thread2.Start(HandleThreadDone);
    
                _count++;
            }
        }
    
        class ThreadWorker
        {
            // Switch to your favourite Action<T> or Func<T>
            public void Run(object state)
            {
                // Do a task
    
                Action completeAction = (Action)state;
                completeAction.Invoke();
            }
        }
    }
    

    If you do use the _count method, it might be an idea (to be safe) to increment it using

    Interlocked.Increment(ref _count)

    I’d be interested to know the difference between using delegates and events for thread notification, the only difference I know are events are called synchronously.


    5. Do it asynchronously instead

    The answer to this question has a very clear description of your options with this method.


    Delegate/Events on the wrong thread

    The event/delegate way of doing things will mean your event handler method is on thread1/thread2 not the main UI thread, so you will need to switch back right at the top of the HandleThreadDone methods:

    // Delegate example
    if (InvokeRequired)
    {
        Invoke(new Action(HandleThreadDone));
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.