简体   繁体   English

Visual Studio 2019 编译和处理代码中的问题 (C#)

[英]Visual Studio 2019 Problem in Compile & Proccess Codes (C#)

I understood that after a Visual Studio 2019 update the timers in C# don't fully work.我了解到,在 Visual Studio 2019 更新后,C# 中的计时器无法完全正常工作。

In my code below, label1 is only set to 3 and nothing after happens.在我下面的代码中, label1仅设置为3 ,之后什么也没有发生。 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.问题不在于 Visual Studio,也不是 2019 版本,也不是 Timer,而是您的代码和算法以及规则。

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.它是调用 OnTick 事件之间的时间量。 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.我只能根据您共享的内容假设您的时间间隔设置在 3001 和 3999 之间。这就是为什么您只看到数字 3 出现在 label 中的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM