There is a method (displays the value of the cell of the array), which must be called at regular intervals. At first I did how. In the loop, I iterated through the array, called the method with the ith argument, and set the Thread.Sleep(1000); They said - you need to get rid of Sleep and use a timer. I threw a timer on the form, hung up the timer on the button press event (initially off), transferred the inside of the loop to the timer, introduced a new array variable, which at the end of the timer increases by 1 and at the very end I stop the timer. I do not understand how to call the timer itself several times with the interval specified in the timer. It works, but only works once (at the touch of a button). And here I am stuck

It was

 private void sayItButton_Click(object sender, EventArgs e) { digit = onDigits(whatToSay.Text); if (digit.Count > 1 && //проверка на то, что у нас не число от digit[digit.Count - 1] >= 1 //11 до 19, а то скажет 10 и 1, вместо 11 && digit[digit.Count - 2] == 10) { digit[digit.Count - 2] += digit[digit.Count - 1]; digit.RemoveAt(digit.Count - 1); } for (int i = 0; i < digit.Count; i++) //проходимся по коллекции и проверяем каждый элемент { if (digit[i] != 0) //проверка на нуль в начале { sayIt(digit[i]); Thread.Sleep(1000); } } flag = true; } 

And it became

  private void timer1_Tick(object sender, EventArgs e) { if (CurDig < digit.Count) { sayIt(digit[CurDig]); CurDig++; } timer1.Stop(); } private void sayItButton_Click(object sender, EventArgs e) { digit = onDigits(whatToSay.Text); if (digit.Count > 1 && //проверка на то, что у нас не число от digit[digit.Count - 1] >= 1 //11 до 19, а то скажет 10 и 1, вместо 11 && digit[digit.Count - 2] == 10) { digit[digit.Count - 2] += digit[digit.Count - 1]; digit.RemoveAt(digit.Count - 1); } while (CurDig < digit.Count - 1) { timer1.Start(); } flag = true; } 
  • 2
    Maybe put a piece of code that reproduces the problem? And then according to the verbal description the code is difficult to read) - Nick Volynkin
  • @NickVolynkin added code before / after - dr_zak
  • Perhaps this answer will help you: Timer in java method - Nick Volynkin
  • one
    @dr_zak: I support the existing answer: you need async and await Task.Delay(...) . - VladD
  • one
    Instead of adding [Решено] to the header, you should accept the answer. - Qwertiy

3 answers 3

It seems that you do not understand a little what a timer is, since you are trying to start it several times in a loop. When the timer is running, it "throws out" tiki events until it is stopped. You stop him right away - so he stops.

Now how do you fix your code.

  1. Remove the loop around timer1.Start(); - it is enough to start the timer only once. In addition, your cycle, as you led it, is infinite, and the program should hang on it ...

  2. Before timer1.Stop(); write else

PS Generally speaking, the task ideally falls on asynchronous programming. If possible, try to use it instead of a timer. All the difference from the original program is that before void in the button click handler write async , and Thread.Sleep(1000) is replaced with await Task.Delay(1000) . Plus, you need logic to block re-pressing a button.

    Thanks a lot to everyone who responded: @Nick Volynkin, @VladD, @Pavel Mayorov, @IGOR. I figured it out myself, sat reading about timers. The result is given below, it may be useful to someone. Self timer

     private void timer1_Tick(object sender, EventArgs e) { if (digit[CurDig] != 0) //проверка на нуль в начале { sayIt(digit[CurDig]); CurDig++; if (CurDig == digit.Count) { timer1.Stop(); CurDig = 0; } } } 

    Button

     private void sayItButton_Click(object sender, EventArgs e) { flag = true; digit = onDigits(whatToSay.Text); if (digit.Count > 1 && //проверка на то, что у нас не число от digit[digit.Count - 1] >= 1 //11 до 19, а то скажет 10 и 1, вместо 11 && digit[digit.Count - 2] == 10) { digit[digit.Count - 2] += digit[digit.Count - 1]; digit.RemoveAt(digit.Count - 1); } timer1.Start(); } 

      The easiest solution!

        private void start_Click(object sender, EventArgs e) { timer1.Start(); timer1.Interval = 1000; } private void stop_Click(object sender, EventArgs e) { timer1.Stop(); } private void timer1_Tick(object sender, EventArgs e) { // метод выводящий значение ячейки массива } 
      • Yes, and I agree with Paul)) First you need to read by Timer !!! - IGOR