C#代码定时器问题 [英] C# code timer problem

查看:93
本文介绍了C#代码定时器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#。我在此代码中遇到问题:

i am using c#. i has problem in this code:

private void Form4_Load(object sender, EventArgs e)
       {
           timer1.Enabled = true;
       }

  private void timer1_Tick(object sender, EventArgs e)
       {
           label1.Text = DateTime.Now.Hour + ":" + DateTime.Now.Minute
                                     + ":" + DateTime.Now.Second;
           string time = DateTime.Now.Hour + ":" + DateTime.Now.Minute
                                          + ":" + DateTime.Now.Second;

           if (time == "17:19:50")
           {
               timer1.Enabled = false;
               Form4 frm3 = this;
               this.Close();
               MessageBox.Show("the time for the program finish");
               Form1 frm1 = new Form1();
               frm1.Show()}
               timer1.Enable =false ;



有时当我运行该代码时,计时器不会执行if语句,为什么???

*** note :我使用c#访问数据库。


sometimes when i run that code the timer do not execute the if statement,why???
***note:i use c# with access database.

推荐答案

首先,它取决于您使用的计时器。如果计时器的分辨率为1000毫秒(1秒),那么它就不能保证在第二秒开启。



例如,如果计时器在第二个#5.5点火,它可能会在第二个6.6时再次触发,它会向上到第二个#7,所以它可以跳过秒。你可以尝试增加你的计时器分辨率来解决这个问题。



这个答案的第二部分是windows形式的计时器非常差。做你想要的最好的方法是检查你想要的时间段(在那个时间之前设置一个标志,然后当那个标志为真,并且在你的触发时间之后,做你的操作)。



如何做到这一点(因为没有旗帜更容易)



First, it depends on the timer that you are using. If the timer has a resolution of 1000ms (1 second) then its not guaranteed to fire on the second.

For example, if the timer fires at second #5.5, it may fire again at second 6.6, which rounds up to second #7, so it can skip seconds. You can try increasing your timer resolution to get around this.

The second part to this answer is that windows forms timers are very poor for timing. The best way to do what you are wanting is to check for the passage through the time that you are wanting (set a flag that its before the time, then when that flag is true and its after your trigger time, do your operation).

How to do this (even easier because no flag)

public class Form4 : Form
{
    DateTime runTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 19, 50, 0);

    public Form4()
    {
        InitializeComponent();
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (DateTime.Now > runTime)
        {
            //Do what you need, then

            runTime = runTime.AddDays(1);
        }
    }
}


您也没有正确比较时间。你要比较两个字符串,而不是两次。



对于这样的时间表你不要比较相等,因为它只相等于1秒。如果在那段时间没有进行比较,它将失败。您比较当前时间以查看它是否大于计划时间,不等于它。现在比较可以随时进行。再次,您比较DateTime对象,而不是字符串来执行此操作。
You're also not comparing the time correctly. You're comparing two strings, not two times.

For a schedule like this you don't compare for equality because it will only be equal for 1 second. If the comparison isn't done in that time, it'll fail. You compare the current time to see if it is greater than the scheduled time, not equal to it. Now the comparison will work at any time. Again, you compare DateTime objects, not strings to do this.


这篇关于C#代码定时器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆