-1

I'm currently doing a project where I need to display few data in a loop. I need to display a data and then pause for a second, then display another and pause and so on. If I add System.Threading.Thread.Sleep(1000) My Application won't load for few seconds and shows me the final value. How else can I code?

int[] val = { 200, 400, 400, 300, 500, 250, 100, 600, 700, 450, 550, 300, 500, 600, 150, 250 };

        high = val.Max();
        low = val.Min();
        for (int i = 0; i < 50; i++)
        {
            ovalShape1.FillColor = getColor(val[0]+ i*5);

            ovalShape2.FillColor = getColor(val[1]+ i*3);
            ovalShape3.FillColor = getColor(val[2]);
            ovalShape4.FillColor = getColor(val[3]);
            ovalShape5.FillColor = getColor(val[4]);
            ovalShape6.FillColor = getColor(val[5]);
            ovalShape7.FillColor = getColor(val[6] + i * 2);
            ovalShape8.FillColor = getColor(val[7] + i * 4);
            ovalShape9.FillColor = getColor(val[8] + i * 2);
            ovalShape10.FillColor = getColor(val[9]);
            ovalShape11.FillColor = getColor(val[10]);
            ovalShape12.FillColor = getColor(val[11]);
            ovalShape13.FillColor = getColor(val[12]);
            ovalShape14.FillColor = getColor(val[13]);
            ovalShape15.FillColor = getColor(val[14]);
            ovalShape16.FillColor = getColor(val[15]);

        }

getColor() is a function that returns some color. This code is just for testing. After each loop of for, i need the color of oval to stay for a second and then change color in next loop.

Thejas B
  • 69
  • 1
  • 2
  • 9

2 Answers2

2

Use System.Windows.Forms.Timer and set the interval to 1000 ms. On every tick, display another data as you mentioned.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
2

You want to use a timer instead of system sleep. DoEvents may also be a good read. Simple syntax example:

    System.Timers.Timer timer = new System.Timers.Timer(2000); 
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        MessageBox.Show("testing the timer"); //load your item here
    }
Hypaethral
  • 1,467
  • 16
  • 22
  • 1
    try to avoid `DoEvents` btw when doing Winforms a backgorund worker or Timer would be a better alternative in my opinion – MethodMan Apr 27 '15 at 17:04