C# 通过计时器显示两个标签一秒钟
我正在用C做一个小游戏# 当分数为100时,我希望两个标签显示一秒钟,然后它们需要再次不可见 目前我在表格1中有:C# 通过计时器显示两个标签一秒钟,c#,timer,label,visible,C#,Timer,Label,Visible,我正在用C做一个小游戏# 当分数为100时,我希望两个标签显示一秒钟,然后它们需要再次不可见 目前我在表格1中有: void startTimer(){ if (snakeScoreLabel.Text == "100"){ timerWIN.Start(); } } private void timerWIN_Tick(object sender, EventArgs e) { int timerTick = 1; if (timerTick == 1) { lb
void startTimer(){
if (snakeScoreLabel.Text == "100"){
timerWIN.Start();
}
}
private void timerWIN_Tick(object sender, EventArgs e)
{
int timerTick = 1;
if (timerTick == 1)
{
lblWin1.Visible=true;
lblWin2.Visible=true;
}
else if (timerTick == 10)
{
lblWin1.Visible = false;
lblWin2.Visible = false;
timerWIN.Stop();
}
timerTick++;
}
计时器的间隔为1000毫秒
问题=标签根本不显示
计时器对我来说很陌生,所以我被困在这里://试试这个:
void startTimer()
{
if (snakeScoreLabel.Text == "100")
{
System.Timers.Timer timer = new System.Timers.Timer(1000) { Enabled = true };
timer.Elapsed += (sender, args) =>
{
lblWin1.Visible=true;
timer.Dispose();
};
}
}
试试这个:
void startTimer()
{
if (snakeScoreLabel.Text == "100")
{
System.Timers.Timer timer = new System.Timers.Timer(1000) { Enabled = true };
timer.Elapsed += (sender, args) =>
{
lblWin1.Visible=true;
timer.Dispose();
};
}
}
尝试使用多线程System.Threading.Timer:
public int TimerTick = 0;
private System.Threading.Timer _timer;
public void StartTimer()
{
label1.Visible = true;
label2.Visible = true;
_timer = new System.Threading.Timer(x =>
{
if (TimerTick == 10)
{
Invoke((Action) (() =>
{
label1.Visible = false;
label2.Visible = false;
}));
_timer.Dispose();
TimerTick = 0;
}
else
{
TimerTick++;
}
}, null, 0, 1000);
}
尝试使用多线程System.Threading.Timer:
public int TimerTick = 0;
private System.Threading.Timer _timer;
public void StartTimer()
{
label1.Visible = true;
label2.Visible = true;
_timer = new System.Threading.Timer(x =>
{
if (TimerTick == 10)
{
Invoke((Action) (() =>
{
label1.Visible = false;
label2.Visible = false;
}));
_timer.Dispose();
TimerTick = 0;
}
else
{
TimerTick++;
}
}, null, 0, 1000);
}
您是否已验证使标签可见的代码是否实际执行?也就是说,您确定执行了
timerWIN\u tick
方法吗?什么设置了timerTick
变量?它不会在某个时刻执行,这实际上是我的问题的一部分:/snakeScoreLabel.Text!=“100”或startTimer()从未运行过。否则,标签应始终显示,因为if(timerTick==1)始终为true。我将打开标签,然后启动计时器,然后在勾号事件处理程序中关闭标签并停止计时器。您是否验证了使标签可见的代码实际执行了?也就是说,您确定执行了timerWIN\u tick
方法吗?什么设置了timerTick
变量?它不会在某个时刻执行,这实际上是我的问题的一部分:/snakeScoreLabel.Text!=“100”或startTimer()从未运行过。否则,标签应始终显示,因为if(timerTick==1)始终为true。我将打开标签,然后启动计时器,然后在tick事件处理程序中关闭标签并停止计时器。