WinForms Timer is incorrect.
-
I'm developing a plant growth simulator. For each plant, there is a specific bulb in the stadium and the duration of one stage. For multiple stages, use a cycle for which the timer method is described and launched. Also timer.Tick and Timer.Stop. The problem is that, instead of switching on and ticking at each stage of growth (i.e., each challenge of the method in the cycle), it is switched on and ticked only at the latter. All the other stages of the method, except Tick. And so the whole chain of action is broken. It looks like:
Initiation of plant mass Initiation of the map set Timer launched Stage 0 passed Timer launched Stage 1 passed Timer launched Stage 2 passed The sunflower's Time 5 Time 4 Time 3 Time 2 Time 1 Time 0 Stage passed
Below is the plant growth cycle. Plants.masPlants. PlantGrowth is a method. Checks and determines the stages of the plant that have been and have not been reached. Returns to the current stage/if the last stage is 13.
for (int i = 0; i < Plants.masPlants[0].CountSteps; i++) { TimerStart(timer0, label_time0, textBox1); var plantgrowstep = Plants.PlantGrowth(Plants.masPlants[0], i, textBox1); PlantPicture.Image = new Bitmap(images[i]); if (plantgrowstep==13) { textBox1.Text += $"Растение {Plants.masPlants[0].NamePlant} выросло\r\n"; //MessageBox.Show($"Растение {Plants.masPlants[0].NamePlant} выросло"); } }
Timer method. It's already been used in another project. Worked, except he didn't call in the cycle.
static int tk, p;
static string c; public static void TimerStart(Timer timer, Label label, TextBox textBox) { p = 5; c = "00:05"; label.Text = c; timer.Interval = 1000; timer.Enabled = true; timer.Start(); textBox.Text += $"Таймер запущен\r\n"; timer.Tick += (sender, args) => { textBox.Text += $"Время {p}\r\n"; tk = --p; TimeSpan span = TimeSpan.FromMinutes(tk); string label_time = span.ToString("hh':'mm"); label.Text = label.ToString(); if (p < 0) { timer.Stop(); textBox.Text += $"Стадия пройдена\r\n"; } }; }
PlantGrowth:
[] plant.GrowSteps is a bulb to describe the stadiums that have been run.public static int PlantGrowth(Plants plant, int i, TextBox textBox)
{
plant.GrowSteps[i] = true;
if (plant.GrowSteps[plant.CountSteps-1]==true)//если последняя стадия роста прошла
{
plant.IsPlantGrown = true;
textBox.Text += $"Стадия {i} пройдена\r\n";
return 13;
}
textBox.Text += $"Стадия {i} пройдена\r\n";
return i;
}
I'm a newcomer, don't judge strictly.
-
That's right.
You're in the DIN and TOT cycle.
There's no way this timer can do anything - like the next cycle is already under way, and you're starting the same timer again.
If you want to use the timer, you need to change the annex architecture, like:
Leave one timer, and already in it (previously in the Tick case treatment method) to start the cycle for all plants.
So you'll have a simulation of one step of growth of the entire plant population on every tick of the timer.