通过计时器显示两个标签一秒钟



我在C#中做了一个小游戏当分数为100时,我希望两个标签显示一秒钟,然后需要再次看不见。

目前我的form1:

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++;
}

计时器的间隔为1000ms。

问题=标签根本没有显示

计时器对我来说很新,所以我被困在这里:/

尝试以下:

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);
        }

最新更新