I understood that after a Visual Studio 2019 update the timers in C# don't fully work.
In my code below, label1
is only set to 3
and nothing after happens. I checked for issues and tested, but I couldn't find the problem
private void Form1_Load(object sender, EventArgs e)
{
Timer1.Start();
}
private void Form1_Enter(object sender, EventArgs e)
{
}
private void Timer1_Tick(object sender, EventArgs e)
{
if (Timer1.Interval >2000)
{
label1.Text = "2";
}
if(Timer1.Interval > 3000)
{
label1.Text = "3";
}
if(Timer1.Interval > 3999)
{
label1.Text = "4";
}
}
As indicated in your comments, you can do that:
private void Timer1_Tick(object sender, EventArgs e)
{
if ( Timer1.Interval > 3999 )
{
label1.Text = "4";
Timer1.Interval = newValue1;
}
else
if ( Timer1.Interval > 3000 )
{
label1.Text = "3";
Timer1.Interval = newValue2;
}
else
if ( Timer1.Interval > 2000 )
{
label1.Text = "2";
Timer1.Interval = newValue3;
}
else
DoSomething();
}
I reversed the conditions tests to be coherent and I added some else to optimize and especially to avoid conflicts.
The problem was not with Visual Studio nor a 2019 version nor a Timer but with your code and the algorythm so the rules.
I am not sure of your goal but you can adapt this corrected code.
The interval on a timer is not the elapsed time since the timer has started. It is the amount of time between when the OnTick event is called. I can only assume based off what you have shared that your interval is set between 3001 and 3999. That is why you are only seeing the number 3 appear in your label.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.