解决  显示时间与实际时间不符

mfwoo

会员
已加入
2020年11月2日
留言内容
7
编程经验
10+
我使用网络摄像头创建了一个倒计时(设置为5秒)的计时器。
它以5、4、3、2、1开头,当其达到0时,我将调用网络摄像头功能以拍摄照片。
秒数通过标签显示。
计时器出现问题,时间间隔不完全是1秒,我可以看到屏幕刷新停止了超过2秒或更长时间,然后标签中的数字发生了变化。

有人可以帮忙吗?

C#:
 public System.Windows.Forms.Timer tmrTakePhotoCountdown = new System.Windows.Forms.Timer();
 tmrTakePhotoCountdown.Interval = 1000;
 tmrTakePhotoCountdown.Tick += new System.EventHandler(this.tmrTakePhoto_Tick);
 
  private void GotoTakePhoto()
  {
        initCamera();
        // start countdown
        int takePhotoCountDown = 5; //5 secs
        CountDown(takePhotoCountDown);
  }
 
  DateTime start;
  int timeLeft;
  public void CountDown(int seconds)
  {
     start = DateTime.Now;
     timeLeft = seconds;

     tmrTakePhotoCountdown.Start();

     lblCountdown.Visible = true;             //label to display seconds to phototake
     lblCountdown.Text = timeLeft.ToString();
  }

   private void tmrTakePhoto_Tick(object sender, EventArgs e)
   {
       lblCountdown.Text = timeLeft.ToString();
       timeLeft -= 1;
       if (timeLeft < 0)
       {
           tmrTakePhotoCountdown.Stop();
           videoCaptureDevice.Stop();
           lblCountdown.Visible = false;
           pixTakePhotoAccept.Visible = true;
           pixTakePhotoRetake.Visible = true;
           pixTakePhotoCancel.Visible = true;
       }
   }
 
Solution
You're doing it very wrong. You should not be using a Timer to measure time. You should be using a Stopwatch. The Timer gives you an opportunity to act by raising an event, much like a Button does, but without the need for user intervention. The two actions you should be performing are using a Stopwatch to measure the remaining time and displaying it, then taking a picture if time has expired.
  1. Declare a field of type TimeSpan and set it to the amunt of tiime you want to count down, e.g. private TiimeSpan countdownTiime = TiimeSpan.FromSeconds(5);
  2. Use a small Interval value for your Timer...

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,463
位置
弗吉尼亚州切萨皮克
编程经验
10+
我可以看到屏幕刷新停止了超过2秒或更长时间,然后标签中的数字更改了
回顾采样法则:您需要将刷新率设置为目标精度的两倍。因此,让您的计时器每500毫秒触发一次,而不是当前的1000毫秒。
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,481
位置
悉尼,澳大利亚
编程经验
10+
You're doing it very wrong. You should not be using a Timer to measure time. You should be using a Stopwatch. The Timer gives you an opportunity to act by raising an event, much like a Button does, but without the need for user intervention. The two actions you should be performing are using a Stopwatch to measure the remaining time and displaying it, then taking a picture if time has expired.
  1. Declare a field of type TimeSpan and set it to the amunt of tiime you want to count down, e.g. private TiimeSpan countdownTiime = TiimeSpan.FromSeconds(5);
  2. Use a small Interval value for your Timer, e.g. 100.
  3. When you start the Tiimer, start a new Stopwatch as well.
  4. When the Timer ticks, subtract the Elapsed time of the Stopwatch from your total time and display the number of seconds remaining.
  5. When the amount of time remaining reaches zero, stop the Timer and do what you need to do.
 
最佳 底部