當程序啓動時,我的計時器'Elapsed'事件觸發兩次。 'Elapsed'事件處理程序的唯一賦值是'Main'方法。有什麼我做錯了嗎?在C#命令行程序中發生兩次定時器事件事件
//class level clock
public static System.Timers.Timer Clock;
static void Main(string[] args)
{
Clock = new System.Timers.Timer();
Clock.Elapsed += new ElapsedEventHandler(Clock_Elapsed);
Clock.AutoReset = false;
Clock.Interval = timerInterval; //this needs to be in milliseconds!
Clock.Enabled = true;
//run infinite loop until q is pressed
while (Console.Read() != 'q')
{}
}
static void Clock_Elapsed(object sender, ElapsedEventArgs e)
{
Clock.Stop();
//do some stuff
Clock.Start();
}
UPDATE:
自動復位由@ fparadis2提供固定擊發兩次。基本問題是我的計時器時間間隔設置爲30毫秒而不是30000毫秒(30秒),因此事件是雙重觸發。
[timer fire twice]可能的重複(http://stackoverflow.com/questions/4309254/timer-fire-twice) – user7116