計時器的時間間隔,我想在另一個線程更改計時器間隔:改變其他線程
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
你試過開始和停止它嗎? –
你的代碼根本不是線程安全的。不知道這是否是您看到帽子的直接原因,但最終會導致問題。 –
開始和停止給出相同的結果 –