Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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# System.Threading.TimerCallback在使用实体框架时仅触发一次_C#_Winforms_Entity Framework_Timer - Fatal编程技术网

C# System.Threading.TimerCallback在使用实体框架时仅触发一次

C# System.Threading.TimerCallback在使用实体框架时仅触发一次,c#,winforms,entity-framework,timer,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

我正在开发一个简单的应用程序,每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
只运行一次。问题是什么?我如何解决?谢谢。

System.Threading.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
        }

删除除计时器外的所有代码。将MessageBox添加到勾号处理程序中。它还只开火一次吗?