C #: executing multiple timers simultaneously

I have three shortcut controls and using For Loop I wrote a method that moves the controls from the top of the form to the bottom. I have three timers, each of which moves one control several times, but the problem is that they execute one after the other.

I want time to run simultaneously, i.e. all three labels move simultaneously from the top of the form to the right.

NOTE. After each timer call, I set a random object that randomly resets the position at the top of the form.

-1
source share
2 answers

Then do not use three timers. Just use one timer and put your code inside it. Tick.

: Random timer1_Tick, :

Random random = new Random(); 
private void timer1_Tick(object sender, EventArgs e)
{
        int X = random.Next(0, 1230); 
        int y = X; 
        label2.Location = new Point(X, 5);
        label3.Location = new Point(X, 5);
        for (int i = 5; i <= 470; i++)
        {
            label2.Location = new Point(y, i);
            label3.Location = new Point(y, i);
            Thread.Sleep(1);
        }
}
+6

, , , . , , .

0

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


All Articles