我和我的朋友正在创建一个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