我用这个代码来刷新我的工作好一次,之后计时器停止了我
private void Button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 900000;//5 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
this.Hide();
Graph1 graph = new Graph1();
graph.Show();
}
您需要将定时器减速移出按钮单击并使其“全局”到该类。另外,在Form_Load上进行设置(确保将Form_Load方法连接到Form_Load事件。
此外,您的隐藏逻辑有点错误。您隐藏表单,然后创建一个图形(但不要将其附加到表单)然后显示它。下面添加了一些评论,以帮助您解决这些问题。
private System.Windows.Forms.Timer timer1;
private void Form_Load(object sender, EventArgs e)
{
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 900000;//5 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
}
private void Button1_Click(object sender, EventArgs e)
{
if (!timer1.Enabled)
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
// Do your data update logic here
this.Refresh();
}