C# Winform Timer倒计时问题 只能用一次

燃燃么 2014-11-16 03:09:44
使用Timer控件做一个倒计时器
可以根据输入的值来控制倒计时的事件。

假设输入的是180s
第一次点开始按钮是 180 179 178 ......正常的
点了重置按钮之后,第二次点开始按钮就是 180 178 176.....
再次重置并且点击开始按钮之后,变成 180 177 174...
...
这些间隔都是1s

这是什么情况?如何解决?

以下为简化版的代码:

private void buttonSetTimeStart_Click(object sender, EventArgs e)
{
this.Clock = 180;
this.timerSetTime.Enabled = true;
this.timerSetTime.Tick += new EventHandler(timerSetTime_Tick);
this.timerSetTime.Start();
}

void timerSetTime_Tick(object sender, EventArgs e)
{
Clock--;
this.labelSetTimeMinute.Text = Clock.ToString();
if (Clock == 0)
{
this.timerSetTime.Stop();
}
}

private void buttonSetTimeReset_Click(object sender, EventArgs e)
{
this.timerSetTime.Enabled = false;
this.timerSetTime.Stop();
this.timerSetTime.Interval = 1000;
this.labelSetTimeMinute.Text = "";
}
...全文
428 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gwygwl_2285662869 2014-11-17
  • 打赏
  • 举报
回复
点选按钮实现任务到计时 //提示10分钟准备倒计时 // 定义在线考试总时间变量index, // 并设置读写属性 private int index { get { object o = ViewState["index "]; return (o == null) ? 600 : (int)o; } set { ViewState["index "] = value; } }//codego.net/tags/11/1/ //计算剩余时间提示 protected void Timer1_Tick(object sender, EventArgs e) { this.index--; if (this.index == 0) //考试时间到了 { this.Timer1.Enabled = false;//设置Timer控件不可见 //时间到时,此处可编写自动提交试卷的方法 } else { //显示考试剩余时间 this.lbTime.Text = this.index / 60 + "分" + this.index % 60 + "秒将停止考试,请及时“提交”试卷,否则试卷作费成绩无效!"; } }
by_封爱 版主 2014-11-17
  • 打赏
  • 举报
回复
引用 5 楼 Z65443344 的回复:
楼上说的都是对的,不过其实如果你只需要一个timer,而不需要代码创建多个timer,放到load里就好了,这句 this.timerSetTime.Tick += new EventHandler(timerSetTime_Tick) 然后按钮里就只是控制start和stop,不要再来回绑定事件,解除事件了
这是正确答案..+=或者是-=你不累代码都累了... 无非就是一个启动或者是停止的事而已...
於黾 2014-11-17
  • 打赏
  • 举报
回复
楼上说的都是对的,不过其实如果你只需要一个timer,而不需要代码创建多个timer,放到load里就好了,这句 this.timerSetTime.Tick += new EventHandler(timerSetTime_Tick) 然后按钮里就只是控制start和stop,不要再来回绑定事件,解除事件了
willhuo 2014-11-17
  • 打赏
  • 举报
回复
膜拜一下
shushukui 2014-11-17
  • 打赏
  • 举报
回复
明显是timer的tick事件重复执行了。
  • 打赏
  • 举报
回复
弄一个timer,控制Enabled就好了,不要了再Close
xian_wwq 2014-11-17
  • 打赏
  • 举报
回复
引用 8 楼 u014531732 的回复:
[quote=引用 1 楼 bdmh 的回复:] 点重置按钮时加上一句代码 this.timerSetTime.Tick -= new EventHandler(timerSetTime_Tick); 解除事件绑定,否则开始按钮中会重复绑定多次
那么如果我启用2个以上的Timer C#会自动做安全工作么?两个线程会不会互相影响?[/quote] 多个timer会不会有影响得看 在timer中处理的数据是不是共享的,如果没有关联 那肯定是安全的 线程是一样的道理
燃燃么 2014-11-17
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
点重置按钮时加上一句代码 this.timerSetTime.Tick -= new EventHandler(timerSetTime_Tick); 解除事件绑定,否则开始按钮中会重复绑定多次
那么如果我启用2个以上的Timer C#会自动做安全工作么?两个线程会不会互相影响?
猴子写代码 2014-11-16
  • 打赏
  • 举报
回复
我去楼上说的没看清除一样的
猴子写代码 2014-11-16
  • 打赏
  • 举报
回复
问题在this.timerSetTime.Tick += new EventHandler(timerSetTime_Tick);这句 这是给委托添加方法,如果没有去掉,会一直往上加,执行的时候会执行委托的所有方法,你可以在你代码中加两句这行代码试试, 解决方法:1:楼上可以解决 2:在重置按钮buttonSetTimeReset_Click 中加上this.timerSetTime.Tick -= new EventHandler(timerSetTime_Tick);
bdmh 2014-11-16
  • 打赏
  • 举报
回复
点重置按钮时加上一句代码 this.timerSetTime.Tick -= new EventHandler(timerSetTime_Tick); 解除事件绑定,否则开始按钮中会重复绑定多次

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧