Windows Stand Alone Application移动到后台时停止自动刷新

时间:2014-02-18 03:50:12

标签: c# .net windows-applications

我有一个Windows独立应用程序,我根据特定的用户定义间隔自动刷新。一切都很好,只有当应用程序在后台并且我在桌面上打开了其他东西时,应用程序才会停止自动刷新。

我正在使用时间进行刷新。这是代码。

Global.DateTime值将获得之前刷新的值。

Global.DateTime = Global.DateTime; 
double Minutes = Convert.ToDouble(Global.dictionary["WebserviceIntervalMins"].ToString()); DateTime refreshDate = Global.DateTime.AddMinutes(Minutes); 
lblRefresh.Text = refreshDate.ToString();

public void InitTimer()
{
    int interv =  Convert.ToInt32(Global.dictionary["WebserviceIntervalMins"].ToString()) * 60000; // Get the interval time from config file and convert minutes into milliseconds
    timer1 = new System.Windows.Forms.Timer();
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = interv; // in miliseconds
    timer1.Start();
}

1 个答案:

答案 0 :(得分:-1)

你能用这样的东西吗?

    bool isRunning = true;
    Thread th = new Thread(delegate()
    {
        while(isRunning)
        {
            BeginInvoke((Action)delegate()
            {
                //do refresh
            });
            int Minutes = Convert.ToInt(Global.dictionary["WebserviceIntervalMins"].ToString());
            Thread.Sleep(Minutes * 60000);
        }
   });
   th.IsBackground = true;
   th.Start();