<分区>
我正在为一个应该精确地使单词标签闪烁的表单编写代码。 它使用“expositionTime”来显示标签 X 毫秒,并使用“intervalTime”来将其隐藏 Y 毫秒。
该过程将重复 Z 次“numExpositions”,并在单击按钮后开始。 我的代码运行没有错误,我使用两个计时器来完成这项工作。 问题是在一些说明之后计时器变得不同步。
我的问题是是否有一个解决方案可以给我精确的时间同步,其中展示时间(显示标签)和间隔(隐藏)将一起开始和结束 - 可能在单独的线程上使用定时器流逝或监控等待和如何实现它们。
我的部分代码使用了两个计时器:
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Timer timer2;
private static int counter = 0;
private static int numExpositions = 16;
private static int expositionTime = 2000;
private static int intervalTime = 1800;
// starts both timers that get desynchronized after some time
private void button1_Click(object sender, EventArgs e) { startTimer1(); startTimer2(); }
public void startTimer1()
{
timer1 = new System.Windows.Forms.Timer() { Interval = expositionTime + intervalTime };
timer1.Tick += new EventHandler(OnTimer1Event);
timer1.Start();
}
public void startTimer2()
{
timer2 = new System.Windows.Forms.Timer() { Interval = intervalTime };
timer2.Tick += new EventHandler(OnTimer2Event);
timer2.Start();
}
private void OnTimer1Event(object sender, EventArgs e)
{
if (counter >= numExpositions) // stops timers
{
timer1.Stop();
timer2.Stop();
wordLabel.Visible = false;
counter = 0;
this.Close()
}
else
{
wordLabel.Visible = true; // shows wordLabel
}
}
private void OnTimer2Event(object sender, EventArgs e)
{
wordLabel.Visible = false; // hides wordLabel
}