计时器的时间间隔,我想在另一个线程更改计时器间隔:改变其他线程
class Context : ApplicationContext {
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Context() {
timer.Interval = 1;
timer.Tick += timer_Tick;
timer.Start();
Thread t = new Thread(ChangeTimerTest);
t.Start();
}
private void ChangeTimerTest() {
System.Diagnostics.Debug.WriteLine("thread run");
timer.Interval = 2;
}
private void timer_Tick(object sender,EventArgs args) {
System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToLongTimeString());
}
}
但是当我改变在新线程的间隔计时器停止。没有错误,计时器停止。 为什么会发生这种情况,我该如何解决?
THX
你试过开始和停止它吗? –
你的代码根本不是线程安全的。不知道这是否是您看到帽子的直接原因,但最终会导致问题。 –
开始和停止给出相同的结果 –