Simple thread test

I want to learn more about streaming processing and create a small test application that will change the background color.

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 get this code to work? I see that the code in ChangeColor is executing, but the color of the label does not change.

+3
source share
4 answers

At first glance, you are creating a new form

Form1 foo = new Form1(); 

inside ThreadTest and never displaying the form, I assume that you intend to change the color of the form in the form using btnStart? You have two options: either pass the form to the ParameterizedThreadStart parameter, or rewrite the code to just work with the existing form.

, , , Invoke , . , - .

Edit

... , ...

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;
    }
  }
} 

, , , , .

tColor.IsBackground = true;

, , ... , .

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();
}
+5

2

a)

b), , ,

, Debug.WriteLine, , .

, BeginInvoke InvokeNeeded

+3

, BackgroundWorker - . , , , , .

BackgroundWorker , . , (UI), , , . , BackgroundWorker .

+1

, google "threading tutorial #", .

winform, , , , win form .

In WinForms, why can't you update user interface controls from other threads?

0
source

Source: https://habr.com/ru/post/1773253/


All Articles