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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:55+00:00 2026-05-28T04:51:55+00:00

can someone help me how to set Thread.join() method within my class or if

  • 0

can someone help me how to set Thread.join() method within my class or if there is a neat way how to deal with SynchronizationContext class and thread.join method. basically, im trying to update a datagridview (dgv) cell and progress bar (pb) every 2 seconds from a different thread (not UI thread). the functionality works fine when one thread does the job; however, i would like to set 2 threads so that the first thread (Thread 1) will update the controls (in my case, it will update the datagridview and display 10 rows and the progress bar will be update to 50%). as soon as thread 1 has done its job, Thread 2 should start and update the controls (in my case, it will update the datagridview and display 10 more rows and the progress bar will be update to 100%). Please see code below.

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace DelegatesAndCallback
{
public partial class Form1 : Form
{
    private Thread newThread1;
    private Thread newThread2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        int id = Thread.CurrentThread.ManagedThreadId;
        Trace.WriteLine("Button thread "+id);

        SynchronizationContext uiContext = SynchronizationContext.Current;

        // thread #1
        startThread1(uiContext);

        // thread #2
        startThread2(uiContext);
    }

    public void startThread1(SynchronizationContext sc)
    {
        // thread #1
        newThread1 = new Thread(Process1) { Name = "Thread 1" };
        newThread1.Start(sc);
        //newThread1.Join();
    }

    public void startThread2(SynchronizationContext sc)
    {
        // thread #2
        newThread2 = new Thread(Process2) { Name = "Thread 2" };
        newThread2.Start(sc);
        //newThread2.Join();
    }

    public  void updateProgressBarValue(object state)
    {
        double val = Convert.ToDouble(state)/19.0;
        pb.Value = (int)(100*val);
    }

    public  void updateDataGridViewValue(object state)
    {
        dgv.Rows.Add((int) state, (int) state);
    }

    public void Process1(object state)
    {
        SynchronizationContext uiContext = state as SynchronizationContext;

        for (int i = 0; i < 10; i++)
        {
            uiContext.Send(updateDataGridViewValue, i);
            uiContext.Send(updateProgressBarValue, i);
            Thread.Sleep(2000);
        }
    }

    public void Process2(object state)
    {
        SynchronizationContext uiContext = state as SynchronizationContext;

        for (int i = 10; i < 20; i++)
        {
            if (uiContext != null) uiContext.Send(updateProgressBarValue, i);
            if (uiContext != null) uiContext.Send(updateDataGridViewValue, i);
            Thread.Sleep(2000);
        }
    }
}
}
  • 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-28T04:51:55+00:00Added an answer on May 28, 2026 at 4:51 am

    To syncronize threads you should use [Manual|Auto]ResetEvents.
    You should use other patterns to write a safe code.
    Investigate my code please:

    public interface IProgress
    {
        ManualResetEvent syncEvent { get; }
        void updateProgressBarValue(int state);
        void updateDataGridViewValue(int state);
    }
    
    public partial class Form1 : Form, IProgress
    {
        // Sync object will be used to syncronize threads
        public ManualResetEvent syncEvent { get; private set; }
    
        public Form1()
        {
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            // Creeate sync object in unsignalled state
            syncEvent = new ManualResetEvent(false);
    
            // I like Async model to start background workers
            // That code will utilize threads from the thread pool
            ((Action<IProgress>)Process1).BeginInvoke(this, null, null);
            ((Action<IProgress>)Process2).BeginInvoke(this, null, null);
        }
    
        public void updateProgressBarValue(int state)
        {
            // InvokeRequired? -> Invoke pattern will prevent UI update from the non UI thread
            if (InvokeRequired)
            {
                // If current thread isn't UI method will invoke into UI thread itself
                Invoke((Action<int>)updateProgressBarValue, state);
                return;
            }
    
            double val = Convert.ToDouble(state) / 19.0;
            pb.Value = (int)(100 * val);
        }
    
        public void updateDataGridViewValue(int state)
        {
            if (InvokeRequired)
            {
                Invoke((Action<int>)updateDataGridViewValue, state);
                return;
            }
    
            dgv.Rows.Add((int)state, (int)state);
        }
    
        public void Process1(IProgress progress)
        {
            for (int i = 0; i < 10; i++)
            {
                // We have InvokeRequired in the methods and don't need any other code to invoke it in UI thread
                progress.updateDataGridViewValue(i);
                progress.updateProgressBarValue(i);
                Thread.Sleep(2000);
            }
    
            // When thread 1 complete its job we will set sync object to signalled state to wake up thread 2
            syncEvent.Set();
        }
    
        public void Process2(IProgress progress)
        {
            // Thread 2 will stop until sync object signalled
            syncEvent.WaitOne();
    
            for (int i = 10; i < 20; i++)
            {
                progress.updateProgressBarValue(i);
                progress.updateDataGridViewValue(i);
                Thread.Sleep(2000);
            }
        }
    }
    

    Code was updated to call UI update from the different classes

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

Sidebar

Related Questions

Can someone help me understanding as why do we call an initialization method on
Can someone help me with my Syntax/Methodology? I would like to set up a
Can someone help me out here? Compiling this code: void test() { std::set<int> test;
can someone help me with my query format. my output is: UPDATE TABLE SET
Can someone help me identify what the purpose of this unidentified syntax is. It
Can someone help me figure out the following make file? BINS=file1 file2 file3 all:
Can someone help explain the following: If I type: a=`ls -l` Then the output
Can someone help me with the getopt function? When I do the following in
Can someone help me with this: This is a program to find all the
Can someone help me to find a solution on how to calculate a cubic

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.