您可以將計時器的Interval
屬性設置爲當前時間與下一課的時間之間的差異;然後在完成課程之後重置差異,以便爲下一課做好準備。但是,這有明顯的缺陷。當你啓動/停止計時器會發生什麼?您需要重置下一課的時間間隔。
或者,你可以做一個定時器,定期檢查是否是時候重新檢查藍牙設備,如果是時間的話。它可能不需要太精確。
// Add your own DateTimes
DateTime[] times = new[] { new DateTime(2010, 4, 20, 16, 30,0,0), new DateTime(2010, 4, 20, 17, 0,0,0) };
Timer t = new Timer();
t.Interval = 30000; // 30 seconds, feel free to change
// Each 30 secs check to see if the _time_ is before one of the ones specified; if it is RunMethod()
t.Tick += (sender, e) => { if (times.Any(d => { DateTime dt = DateTime.Now; new DateTime(dt.Year, dt.Month, dt.Day, d.Hour, d.Minute, d.Second, d.Millisecond).CompareTo(dt) <= 0 })) RunMethod(); }
是我必須停止計時器或不?另外如果我不停止計時器會導致性能問題?因爲這個程序可能會在服務器上工作,可能會出現漏洞。 – Ercan 2010-04-19 22:34:44