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; }
async
andawait Task.Delay(...)
. - VladD[Решено]
to the header, you should accept the answer. - Qwertiy ♦