【发布时间】:2012-11-16 07:23:26
【问题描述】:
我的代码中有一个Windows.Forms.Timer,我正在执行 3 次。但是,计时器根本没有调用 tick 函数。
private int count = 3;
private timer;
void Loopy(int times)
{
count = times;
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
count--;
if (count == 0) timer.Stop();
else
{
// Do something here
}
}
Loopy() 正在从代码中的其他位置调用。
【问题讨论】:
-
你从哪里调用 Loopy?
-
Loopy() 正在从代码中的另一个位置调用。
-
从其他地方调用它会覆盖当前的计时器对象。有你的问题。
-
您如何验证计时器没有计时?你在哪里调用 Loopy 函数?最好提供一个最小但完整的示例来说明问题。到时候你很可能会自己解决。
-
这段代码很好用。问题出在您未显示的代码中的其他地方。