我和我的朋友正在創建一個podcastplayer。每隔30分鐘,60分鐘或2小時,程序應該查看rss feed並查看是否發佈了新劇集。如果是這樣,我們節目中的劇集列表應該隨着新劇集的添加而更新。因此,現在我們試圖使用System.Timers.Timer類來設置執行我們的方法來查找新劇集的時間間隔。爲了測試我們的方法,我們只想每10秒打印出一個消息框。但是在10秒之後,該程序只是不斷髮現新的消息框。我們如何重置計時器並在10秒後顯示一個新的消息框?是因爲我們使用了一個消息框嗎?如果我們除了顯示一個消息框之外還有別的東西,定時器是否會重置?我們嘗試將信息輸出到控制檯,但同樣的問題發生。C#定時器不顯示消息框後重置,只是每秒顯示一個新的消息框
這裏是我們的代碼:
using System;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
public static class TimerInitializer
{
public static Timer timer; // From System.Timers
public static void Start()
{
timer = new Timer(10000); // Set up the timer for 10 seconds
//
// Type "_timer.Elapsed += " and press tab twice.
//
timer.Elapsed += new ElapsedEventHandler(timerElapsed);
timer.Enabled = true; // Enable it
}
public static void timerElapsed(object sender, ElapsedEventArgs e)
{
MessageBox.Show("Hello");
}
}
不能確定你的問題,但我認爲你需要停止計時器,然後顯示消息框後,再次啓動它,像'timer.Enabled = FALSE;的MessageBox.show( 「你好」); timer.Enabled = true;' – Habib 2014-10-27 14:33:46