System.Threading.TimerCallback仅在使用Entity Framework时触发一次

时间:2015-01-25 06:41:18

标签: c# winforms entity-framework timer

我正在开发一个每60秒检查一次数据库的简单应用程序。我使用System.Threading.TimerCallback来做到这一点,但是,当我运行应用程序时,它只会打勾一次。

以下是代码:

private void Form1_Load(object sender, EventArgs e)
    {
        // Create the delegate for the Timer type. 
        System.Threading.TimerCallback timeCB = new System.Threading.TimerCallback(d);

        // Establish timer settings. 
        System.Threading.Timer t = new System.Threading.Timer(
          timeCB,     // The TimerCallback delegate object. 
          null,       // Any info to pass into the called method (null for no info). 
          0,          // Amount of time to wait before starting (in milliseconds). 
          1000);      // Interval of time between calls (in milliseconds). 
    }

    void m(object o)
    {
        System.Media.SystemSounds.Hand.Play();
        SReminderEntities ctx = new SReminderEntities();

        var jobs = (from m in ctx.Messages
                    select m).ToList();

        var seljobs = from j in jobs
                      where j.RemindeTime.Date == DateTime.Now.Date
                            && j.RemindeTime.Hour == DateTime.Now.Hour
                            && j.RemindeTime.Minute == DateTime.Now.Minute
                      select j;

        foreach (var j in seljobs)
        {
           // SendGmail("iReminder", j.Text, new string[] { j.User.Email }, "iReminderSender@Gmail.com");
            //this.sendSMS(j.User.Mobile, j.Text);
            System.Media.SystemSounds.Hand.Play();
        }//foreach
    }

    void d(object o)
    {
        MessageBox.Show("Test");
    }

当我致电d时,它有效,但m只运行一次。有什么问题,如何解决?谢谢。

2 个答案:

答案 0 :(得分:4)

System.Threading.Timer类要求您保留自己的引用。它本身没有任何根数据结构可以保持实例可访问。因此,如果您没有自己保留引用,垃圾收集器将回收该对象,丢弃您的计时器。

尝试在表单类中创建一个私有实例字段,并在那里存储引用。

另见the documentation,其中部分内容为:

  

只要您使用Timer,就必须保留对它的引用。与任何托管对象一样,当没有对它的引用时,Timer会进行垃圾回收。定时器仍处于活动状态这一事实并不能阻止它被收集。

答案 1 :(得分:0)

一旦完成Form_Load,它就会自行处理。您需要创建对Timer对象的窗体级别引用,然后使用所需的参数在Form_Load上重新构造它。

System.Threading.Timer t = System.Threading.Timer    

private void Form1_Load(object sender, EventArgs e)
        {
            // Create the delegate for the Timer type. 
            System.Threading.TimerCallback timeCB = new System.Threading.TimerCallback(d);

            // Establish timer settings. 
              t = new System.Threading.Timer(
              timeCB,     // The TimerCallback delegate object. 
              null,       // Any info to pass into the called method (null for no info). 
              0,          // Amount of time to wait before starting (in milliseconds). 
              1000);      // Interval of time between calls (in milliseconds). 
        }

        void m(object o)
        {
            System.Media.SystemSounds.Hand.Play();
            SReminderEntities ctx = new SReminderEntities();

            var jobs = (from m in ctx.Messages
                        select m).ToList();

            var seljobs = from j in jobs
                          where j.RemindeTime.Date == DateTime.Now.Date
                                && j.RemindeTime.Hour == DateTime.Now.Hour
                                && j.RemindeTime.Minute == DateTime.Now.Minute
                          select j;

            foreach (var j in seljobs)
            {
               // SendGmail("iReminder", j.Text, new string[] { j.User.Email }, "iReminderSender@Gmail.com");
                //this.sendSMS(j.User.Mobile, j.Text);
                System.Media.SystemSounds.Hand.Play();
            }//foreach
        }