Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过计时器显示两个标签一秒钟_C#_Timer_Label_Visible - Fatal编程技术网

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

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

我正在用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)
  {
    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事件处理程序中关闭标签并停止计时器。