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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:59:59+00:00 2026-05-30T15:59:59+00:00

I am working on an application where two threads execute a method from a

  • 0

I am working on an application where two threads execute a method from a class simultaneously. This method performs some complex calculations and the result is displayed in a WinForm.

So my class looks like:

class Comp_func
{
    /* Constructor */
    public Comp_func()
    {
    }

    public void THE_Complex_func(string One, Int16 Two, Int32 Three)
    {
        Calculate_this(One);
        Thread.Sleep(10);
        Calculate_that(Two);
        Thread.Sleep(10);
        Calculate_thatThat(Three);
        Thread.Sleep(10);
        Update_lable_in_form(value);
        Thread.Sleep(10);
    }
}

Each thread updates different Label in the WinForm so, I expect no issues about thread synchronization (as I have used Thread Safe Methods).

My need is those updates should occur simultaneously (at least seem to occur simultaneously).

When the user clicks on a “Calculate” button in my form, I do:

Comp_func Func1_class = new Comp_func();
Comp_func Func2_class = new Comp_func();

Thread Func1_Thread = new Thread(() => Func1_class.Start_Test("Blah", 2,3));
Thread Func2_Thread = new Thread(() => Func2_class.Start_Test("BlahBlah", 4,5));

//Func1_Thread.Priority = ThreadPriority.Highest;  /* Deleted after Comments */
   // Func2_Thread.Priority = ThreadPriority.Highest;  /* Deleted after Comments */


Func1_Thread.Start();
Func2_Thread.Start();

/*
  //  Removed after Comments 

// Let only Threads Run
 while(true)
{
    Application.DoEvent();
    Update();
}

for(count = 0; count < 100; count++)
 {  
    console.WriteLine("I WON'T USE WHILE(1) loops with DOEVENTS");
 }

*/

My problem is that when execution of Thread 1 completes, it updates the Label, and then Thread 2 starts and executes.

In order to avoid this behavior, I wrote Thread.Sleep(); after almost each line. It still behaves as if windows did not take care of switching!

Any idea why this is happening?

Thanks in Advance!

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

    Your code example is quite incomplete – so it is difficult to see what is going on.

    the first comment I would have is that you should not have an infinite loop in your click handler – in fact you shouldn’t need the while(true)..DoEvent() code at all

    In response to your requirement that the two labels update simultaneously the best thing to do is have the threads join before updating the UI – one method to do this would be to create a BackgroundWorker that kicks off the other two threads, Join()s them, then updates the UI with the two results – depending on the version of .net you are running you may be able to use the Tasks library to do it with less code.

    here is a rather crude implementation (if you don’t have the Tasks library available):

        private void button1_Click(object sender, System.EventArgs e)
        {
            BackgroundWorker master= new BackgroundWorker();
            master.DoWork += (sender1, e1) =>
                                  {
                                      Thread t1 = new Thread(Func1_class.Start_Test);
                                      Thread t2 = new Thread(Func1_class.Start_Test);
                                      t1.Start();
                                      t2.Start();
                                      t1.Join();
                                      t2.Join();
                                  };
            master.RunWorkerCompleted += (sender2, e2) =>
                                              {
                                                  label1.Text = "text";
                                                  label2.Text = "text";
                                              };
            master.RunWorkerAsync();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a multi-threaded Silverlight application. The application has two threads: Main/UI and
I'm working on an image processing application where I have two threads on top
I'm working on a Rails application where I have some a set of two
I've been working on an application for a little over two years and have
I am working on a simple app tab-bar based application that has two views.
I'm working on a WPF application which should be utilizable with two monitors. In
I'm working on a solution that has two projects: One ASP.NET web application, and
I'm slowly building my application to a working state. I'm using two functions called
I'm currently working on an Asp.net MVC Web application. In my solution, there're two
I'm working on a graphical application which looks something like this: while (Simulator.simulating) {

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.