[英]Visual Studio 2019 Problem in Compile & Proccess Codes (C#)
我了解到,在 Visual Studio 2019 更新后,C# 中的計時器無法完全正常工作。
在我下面的代碼中, label1
僅設置為3
,之后什么也沒有發生。 我檢查了問題並測試了,但我找不到問題
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";
}
}
如您的評論中所示,您可以這樣做:
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();
}
我顛倒了條件測試以保持一致,並添加了一些其他內容來優化,尤其是避免沖突。
問題不在於 Visual Studio,也不是 2019 版本,也不是 Timer,而是您的代碼和算法以及規則。
我不確定你的目標,但你可以調整這個更正的代碼。
計時器上的時間間隔不是自計時器啟動以來經過的時間。 它是調用 OnTick 事件之間的時間量。 我只能根據您共享的內容假設您的時間間隔設置在 3001 和 3999 之間。這就是為什么您只看到數字 3 出現在 label 中的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.