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

  • Home
  • SEARCH
  • 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 3339858
In Process

The Archive Base Latest Questions

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

I want to learn more about threading and created a little test application that

  • 0

I want to learn more about threading and created a little test application that change the backcolor of a label.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    //lblColor
    public Color theLabel
    {
        get { return this.lblColor.BackColor;  }
        set { this.lblColor.BackColor = value; }
    }

    //btnStart
    private void btnStart_Click(object sender, EventArgs e)
    {
        ThreadTest cColor = new ThreadTest();
        Thread tColor = new Thread(new ThreadStart(cColor.ChangeColor));

        tColor.Start();
    }
}

And…

public class ThreadTest
{
    public void ChangeColor()
    {
        Form1 foo = new Form1();
        while (true)
        {
            foo.theLabel = Color.Aqua;
            foo.theLabel = Color.Black;
            foo.theLabel = Color.DarkKhaki;
            foo.theLabel = Color.Green;
        }
     }
}

The only problem is why can’t i make this code work? I can see that the code in ChangeColor runs but the color of the label don’t change.

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

    At first glance, you are constructing a new form

    Form1 foo = new Form1(); 
    

    inside ThreadTest and never displaying the form, my guess is you intended to change the form color on the form with btnStart? You have two options, either pass in the form in to a ParameterizedThreadStart or re-write the code to just operate on the existing form.

    Also, based on the code written, you will likely need to use Invoke to update the state of the form as you cannot have worker threads update the UI. I will tweak your code and posted a revised example if someone doesn’t beat me to it.

    Edit

    In this case you don’t need the invoke… but here is what I think you were intending…

    private void btnStart_Click(object sender, EventArgs e)
    {
      ThreadTest cColor = new ThreadTest();
      Thread tColor = new Thread(new ParameterizedThreadStart(cColor.ChangeColor));
    
      tColor.Start(this);
    }
    
    public class ThreadTest
    {
      public void ChangeColor(Object state)
      {
        Form1 foo = (Form1) state;
        while (true)
        {
          foo.theLabel = Color.Aqua;
          foo.theLabel = Color.Black;
          foo.theLabel = Color.DarkKhaki;
          foo.theLabel = Color.Green;
        }
      }
    } 
    

    Also, it is important to set worker threads as background threads, otherwise when you close the form, the thread will keep that appliaction open.

    tColor.IsBackground = true;
    

    Additional Example

    A slightly different example would be to have multiple threads trying to update the same value and see how they are interleaved… simple snippet to get the ball rolling.

    private void btnStart_Click(object sender, EventArgs e)
    {
      CreateBackgroundColorSetter(Color.Aqua);
      CreateBackgroundColorSetter(Color.Black);
      CreateBackgroundColorSetter(Color.DarkKhaki);
      CreateBackgroundColorSetter(Color.Green);
    }
    
    
    private void CreateBackgroundColorSetter(Color color)
    {
      var thread = new Thread(() =>
                                {
                                  while (true)
                                  {
                                    theLabel = color;
                                    Thread.Sleep(100);
                                  }
                                });
      thread.IsBackground = true;
    
      thread.Start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to go backwards and learn more about how compilers, processors and memory
I want to learn more about using the SSE . What ways are there
I want to learn more about replication and want to turn it on for
I want to learn more about creating my own, custom WS_CAPTION style, the end
I really want to learn more about C++. I know the basics, and I
As apart of my learning, I want to learn more about the data controls
It's not too pricey and I want to learn more about design patterns. Is
I want to learn how to use Struts 2 and I created a simple
I currently learning about scrum and want to learn from experienced professionals in the
I'm building a toy database in C# to learn more about compiler, optimizer, and

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.