1

when i click the button, light brightness Slowly up to 25, and then slowly down to 0. But There is something problem. After i click the button , i couldn't control other buttons Until Light brightness program is finished. I think it is cause of Thread problem. but I'm new to C#, so I have no idea how can i solve it. Could you give some solution?

Here is my code

   int i;
    private void button2_Click(object sender, EventArgs e)
    {

        for (i = 0; i < 25; i++)
        {
        byte[] Send_data = new byte[7];
        Send_data[0] = 0x02;
        Send_data[1] = Convert.ToByte(textBox3.Text);
        Send_data[2] = Convert.ToByte(textBox4.Text);


        Send_data[3] = Convert.ToByte(i);
        Thread.Sleep(30);

        Send_data[4] = Convert.ToByte(textBox6.Text);
        Send_data[5] = 0x04;

        int test = 0xff & (Send_data[0] + Send_data[1] + Send_data[2] + Send_data[3] + Send_data[4] + Send_data[5]);
        Send_data[6] = (byte)test;
        serialPort.Write(Send_data, 0, 7);
        }
        for (i = 24; i > 0; i--)
        {
            byte[] Send_data = new byte[7];
            Send_data[0] = 0x02;
            Send_data[1] = Convert.ToByte(textBox3.Text);
            Send_data[2] = Convert.ToByte(textBox4.Text);


            Send_data[3] = Convert.ToByte(i);
            Thread.Sleep(30);

            Send_data[4] = Convert.ToByte(textBox6.Text);
            Send_data[5] = 0x04;

            int test = 0xff & (Send_data[0] + Send_data[1] + Send_data[2] + Send_data[3] + Send_data[4] + Send_data[5]);
            Send_data[6] = (byte)test;
            serialPort.Write(Send_data, 0, 7);

        }


    }
stuartd
  • 66,195
  • 14
  • 128
  • 158
  • Calling this Post a duplicate because it has already been answered using `BackgroundWorker` implies that using `BackgroundWorker` would be the correct solution. I disagree. There are `Async` possibilities as well as the TPL or even a good old fashioned `Thread`. That said--I would agree that this question is to broad and any answer would be opinion. – Barns May 28 '20 at 02:15

0 Answers0