我有一个Windows应用程序,它在屏幕上使用计时器: 计时器,它用于在08:00:00(开始时间)和21:00:00(停止时间)之间每X分钟运行一个方法 在屏幕上: 下次下载起始于:显示下次运行的时间 剩余时间:显示到下次运行为止的剩余时间 下载间隔:两次运行之间的时间跨度
该方法在下次下载开始=当前时间时运行。
我的问题是该应用程序已打开并运行,计时器在一段时间后才停止。 为什么会发生这种情况,这是正常现象,还是我在代码中做错了什么(我可以在需要时将其发布,但是现在我只是想看看它是否正常)? 有什么方法可以阻止它,或者我可以创建我自己的检查器并定期检查其状态?
提前感谢您的支持!
private System.Windows.Forms.Timer timerFrequency = new System.Windows.Forms.Timer();
public void LoadGeneraInfoPanel()
{
timerFrequency.Interval = 1000;
timerFrequency.Enabled = true;
this.timerFrequency.Tick += new System.EventHandler(this.timerFrequency_Tick);
timerFrequency.Start();
}
private void timerFrequency_Tick(object sender, EventArgs e)
{
txtGICurrTime.Text = DateTime.Now.ToString("HH:mm:ss");
if (drSystemParam["Auto"].ToString() == "True" && timerFrequency.Enabled)
{
if (txtGIFrequency.Text.ToString() != string.Empty && txtGIStopTime.Text.ToString() != string.Empty && txtGIStartTime.Text.ToString() != string.Empty)
{
int iHours = Convert.ToInt32(txtGIFrequency.Text.Substring(0, 2));
int iMinutes = Convert.ToInt32(txtGIFrequency.Text.Substring(3, 2));
int iSeconds = Convert.ToInt32(txtGIFrequency.Text.Substring(6, 2));
DateTime dCurrStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(txtGIStartTime.Text.Substring(0, 2)), Convert.ToInt32(txtGIStartTime.Text.Substring(3, 2)), Convert.ToInt32(txtGIStartTime.Text.Substring(6, 2)));
DateTime dCurrStopTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(txtGIStopTime.Text.Substring(0, 2)), Convert.ToInt32(txtGIStopTime.Text.Substring(3, 2)), Convert.ToInt32(txtGIStopTime.Text.Substring(6, 2)));
DateTime dCurrStartDownloadTime = dCurrStartTime;
for (int j = 0; dCurrStartDownloadTime < DateTime.Now & dCurrStartDownloadTime <= dCurrStopTime; j++)
dCurrStartDownloadTime = dCurrStartDownloadTime.AddHours(iHours).AddMinutes(iMinutes).AddSeconds(iSeconds);
var result = dCurrStartDownloadTime;
if (DateTime.Now > dCurrStartTime && DateTime.Now < dCurrStopTime.AddHours(-iHours).AddMinutes(-iMinutes).AddSeconds(-iSeconds))
{
if (txtNextDownloadTime.Text == String.Empty)
txtNextDownloadTime.Text = result.ToString("HH:mm:ss");
TimeSpan diff = result - DateTime.Now.AddSeconds(-1);
txtGITimeLeft.Text = string.Format("{0}:{1}:{2}", diff.Hours.ToString().PadLeft(2, '0'), diff.Minutes.ToString().PadLeft(2, '0'), diff.Seconds.ToString().PadLeft(2, '0'));
if (DateTime.Now.ToString("HH:mm:ss") == txtNextDownloadTime.Text)
{
timerFrequency.Stop();
txtNextDownloadTime.Text = result.ToString("HH:mm:ss");
Thread.Sleep(800);
timerFrequency.Enabled = true;
timerFrequency.Start();
btnEecuteMethod_Click(sender, e);
}
}
else
{
result = dCurrStartTime.AddDays(1);
txtNextDownloadTime.Text = result.ToString("HH:mm:ss");
TimeSpan diff = result - DateTime.Now.AddSeconds(-1);
txtGITimeLeft.Text = string.Format("{0}:{1}:{2}", diff.Hours.ToString().PadLeft(2, '0'), diff.Minutes.ToString().PadLeft(2, '0'), diff.Seconds.ToString().PadLeft(2, '0'));
}
}
else
{
timerFrequency.Enabled = false;
timerFrequency.Stop();
}
}
}