我使用下面的代码每24小时调用一次网页。据我所知,Timer.Elapsed事件应该在24小时内触发一次。不幸的是,有些时候timer.Elapsed从不同的线程连续发射数次。谁能解释为什么会发生这种情况?定时器ASP.NET多线程问题
void Application_Start(object sender, EventArgs e)
{
var timer = new System.Timers.Timer { Interval = 1 };
timer.Elapsed += delegate
{
SiAuto.Main.LogMessage("Scheduler Timer");
timer.Interval = TimeSpan.FromHours(24).TotalMilliseconds;
var webClient = new WebClient();
webClient.DownloadDataAsync(new Uri(WebHelper.GetBaseUrl + "/scheduled-task"));
};
timer.Start();
}
难道是我必须将间隔= 1的开始间隔更改为更大的间隔= 1000? – Tomas
您确定您的网站保持24小时(或更长时间)吗? IIS关闭没有流量的网站。 –
这是发生在Web应用程序内部吗?使用Web应用程序进行任务调度通常不是一个好主意。 Windows服务或预定的控制台应用程序或类似的东西会更可靠。该网站的应用程序是在网络服务器的怜悯之下,它的启动时间,运行时间等。 – David