此计时器在设置为3秒,10秒,60秒,10分钟,20分钟时会激发,但在设置为30分钟时将停止工作。这只是一个简单的应用程序,会弹出我的屏幕,提醒我执行各种任务。我看过有关使用系统计时器,线程计时器和窗体计时器的文章。但是,由于该特定应用程序实际上是要与UI交互的,所以我认为Forms Timer是必经之路。此外,该代码似乎可以在30分钟以下的时间内完美运行。
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
static int thirtyMins = 30 * 60 * 1000;
static int runTime = thirtyMins;
static int minTotal = 0;
public Form1()
{
InitializeComponent();
}
public void updateLabels()
{
label1.Text = "shown " + DateTime.Now.ToString("h:mm:ss tt");
label2.Text = runTime / 1000 + " sec";
label3.Text = "hidden " + minTotal;
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = thirtyMins * 1000;
timer1.Enabled = true;
updateLabels();
Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt") + " Loaded!!");
}
private void timer1_Tick(object sender, EventArgs e)
{
updateLabels();
this.WindowState = FormWindowState.Normal;
this.Activate();
this.Update();
Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt") + " Tick!!");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
if (textBox1.Text.Contains("*"))
{
int sec = 1;
String[] split = textBox1.Text.Split('*');
foreach (string s in split)
{
sec *= Int32.Parse(s.Trim());
}
runTime = sec * 1000;
}
else
{
runTime = Int32.Parse(textBox1.Text.Trim()) * 1000;
}
timer1.Interval = runTime;
updateLabels();
}
catch
{
timer1.Interval = thirtyMins;
}
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
minTotal++;
updateLabels();
}
}
}
}