Traffic light in c # using threads

0

When I start the thread do not even change the images before I get to the sleep, how do I put the thread correctly, or what change do I need to make it work?

using System;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace semaforo_hilos_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int c = 1;
        public void Go()
        {
            for (int i = 1; i <= 10; i++)
            {
                if (c == 1)
                {
                    pictureBox1.Image = Properties.Resources.gris;
                    pictureBox2.Image = Properties.Resources.gris;
                    pictureBox3.Image = Properties.Resources.verde;
                    c = 2;
                    Thread.Sleep(3000);

                }
                if (c == 2)
                {
                    pictureBox1.Image = Properties.Resources.gris;
                    pictureBox2.Image = Properties.Resources.amarillo;
                    pictureBox3.Image = Properties.Resources.gris;
                    c = 3;
                    Thread.Sleep(3000);
                }
                if (c == 3)
                {
                    pictureBox1.Image = Properties.Resources.rojo;
                    pictureBox2.Image = Properties.Resources.gris;
                    pictureBox3.Image = Properties.Resources.gris;
                    c = 1;
                    Thread.Sleep(3000);
                }

            }


        }

         public void Form1_Load(object sender, EventArgs e)
        { 
              Form1 obj = new Form1();
              Thread t1 = new Thread(new ThreadStart(obj.Go));
              t1.Start();

        }
    }
}
    
asked by Antonio Salgado 31.10.2017 в 02:49
source

1 answer

2

Your immediate problem is in the following lines:

Form1 obj = new Form1();
Thread t1 = new Thread(new ThreadStart(obj.Go));

You are running the Go method in a different instance of Form1 that is not visible. Rather, you must execute Go in the current instance of the form, that is, this :

Thread t1 = new Thread(new ThreadStart(this.Go));

With this small change, you should notice that things work better.

Now, I want to warn you that it is not good practice to manipulate visual controls from any thread. Normally you should make the main thread (UI thread). Otherwise, it often throws you error or there may be weird behaviors. I warn you to get used to using the best practices.

In this case, it would probably be better to use a Timer, or some similar mechanism.

Assuming that you add a Timer to your form, here is an example of how you could achieve the same without violating the basic rule of manipulating the controls from the main thread only.

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

    private void Form1_Load(object sender, EventArgs e)
    {
        this.CambiarImagenes();
        this.timer1.Interval = 3000;
        this.timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.CambiarImagenes();

        if (i >= 10)
        {
            timer1.Stop();
        }
    }

    private int i = 0;
    private int c = 1;

    private void CambiarImagenes()
    {
        switch (c)
        {
            case 1:
                pictureBox1.Image = Properties.Resources.gris;
                pictureBox2.Image = Properties.Resources.gris;
                pictureBox3.Image = Properties.Resources.verde;
                c = 2;
                break;
            case 2:
                pictureBox1.Image = Properties.Resources.gris;
                pictureBox2.Image = Properties.Resources.amarillo;
                pictureBox3.Image = Properties.Resources.gris;
                c = 3;
                break;
            case 3:
                pictureBox1.Image = Properties.Resources.rojo;
                pictureBox2.Image = Properties.Resources.gris;
                pictureBox3.Image = Properties.Resources.gris;
                c = 1;
                break;
        }

        i++;
    }
}
    
answered by 31.10.2017 / 03:31
source