在计时器与我一起停止运行后,我使用这段代码刷新了我的工作一次
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();
}
答案 0 :(得分:0)
您需要将定时器的减速度从按钮单击中移出,并使其成为“全局”类。另外,在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();
}