0

By following code(the interval of timer2 is 1000)

private void timer1_Tick(object sender, EventArgs e) {
    timer7.Enabled=false;
    timer8.Enabled=false;
    lblTimer_Value_InBuildings.Text="0";
}

private void timer2_Tick(object sender, EventArgs e) {
    lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}

we can not create a delay in a for-loop

for(int i=1; i<=Max_Step; i++) { 
    // my code... 

    // I want delay here: 
    timer1.Interval=60000; 
    timer1.Enabled=true; 
    timer2.Enabled=true; 

    // Thread.Sleep(60000); // makes no change even if uncommenting
}

Whether I uncomment the line Thread.Sleep(60000); or not, we see nothing changed with lblTimer_Value_InBuildings in timer2_Tick.

Would you please give me a solution(with or without timers)?

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
SilverLight
  • 19,668
  • 65
  • 192
  • 300

3 Answers3

5

Your timer is your loop, you don't need a for loop. You just keep track of your loop variables outside of the function calls. I would recommend wrapping all of this functionality into a class, to keep it separate from your GUI code.

private int loopVar = 0;
public void Form_Load()
{
    // Start 100ms after form load.
    timer1.Interval = 100;
    timer1.Enabled = true;
}


private void timer1_Tick(object sender, EventArgs e)
{
   timer1.Enabled = false;
   //  My Code Here
   loopVar++;

   if (loopVar < Max_Step)
   {
      // Come back to the _tick after 60 seconds.
      timer1.Interval = 60000;
      timer1.Enabled = true;

   }
}
John Koerner
  • 37,428
  • 8
  • 84
  • 134
2

When you do Thread.Sleep(60000), you are telling the UI thread to sleep for 60 seconds. What this also does is prevent the execution of the timer, because the UI thread is hung up sleeping instead of processing events such as the timer sleep.

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40
  • thanks for the attention bro, but this is not an answer. please replace it with a comment! – SilverLight Feb 02 '13 at 00:01
  • 1
    @MoonLight Uh, this is quite clearly an answer. Also, he's pretty much correct in what he tells you is wrong. – David Pfeffer Feb 02 '13 at 00:18
  • 1
    Sorry @MoonLight, next time I'll try not to actually tell you WHY your code isn't working. I'll just do all your work for you. – Pete Garafano Feb 02 '13 at 00:23
  • @BrianRasmussen Based on context, it would appear that these are WinForms/WPF timers, not threading timers. So yes, if the sleep is called from the UI thread, the timer would not be executed until the sleep completes because the UI is not processing messages. – David Pfeffer Feb 02 '13 at 01:08
1

The code totally made me feel boring

methods:

private void timer1_Tick(object sender, EventArgs e) {
    timer2.Enabled=false;
    timer1.Enabled=false;
    lblTimer_Value_InBuildings.Text="0";
}

private void timer2_Tick(object sender, EventArgs e) {
    lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}

private void timer3_Tick(object sender, EventArgs e) {
    if(0!=(int)timer3.Tag) {
        // your code goes here and peformed per step
        timer1.Enabled=true;
        timer2.Enabled=true;
    }

    timer3.Tag=(1+(int)timer3.Tag)%Max_Step;
}

initials:

var delayedInterval=60000;

timer1.Interval=60000;
timer2.Interval=1000;
timer3.Interval=delayedInterval+timer1.Interval;

lblTimer_Value_InBuildings.Text="0";
timer3.Tag=1;
timer3.Enabled=true;

Your original code never makes timer1 and timer2 stopped, so I think you should correct timer7 and timer8 to timer1 and timer2, otherwise they don't make sense here.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
  • i could n't understand, what is the benefit of timer3 - mean where is that delay in your codes? – SilverLight Feb 02 '13 at 11:29
  • Your code shown that it's a winform application, that is, your UI thread has a `SynchronizationContext`. How timers work is a little bit complicated to explain, but `Sleep` a thread just blocks other threads expecting to get objects synchronized with it. – Ken Kin Feb 02 '13 at 21:42