【发布时间】:2012-11-18 19:41:13
【问题描述】:
例如,我想在特定的时间间隔(每小时或每天的任务)调用一个函数。
我已经在使用定时器了。但只要我的本地服务器正常工作,它就可以工作。例如:
public static void StartYahooWeatherManager()
{
Timer t = new Timer(1000 * 60 * 60 * 6 /* 6 hours */);
t.Elapsed += new ElapsedEventHandler(WriteYahoWeatherRssItemToDb);
t.Start();
}
private static void WriteYahoWeatherRssItemToDb(object o, ElapsedEventArgs e)
{
YahooWeatherManager.InsertYahooWeatherRssItem(YahooWeatherManager.GetYahooWeatherRssItem());
}
我每隔 6 小时将天气信息写入 db。项目已经在主机中。只要我的计算机本地服务器正在运行,计时器事件就会起作用。
问题:
我的代码有什么问题? (或者在 WebProject 中使用定时器逻辑错误?)为什么定时器只在本地服务器工作时工作?
管理时间间隔任务的最佳方式是什么? (不需要asp.net)
我希望,我可以解释这个问题。谢谢。
【问题讨论】:
-
你真的需要在 ASP.NET 应用程序中这样做吗?您是否考虑过创建 Windows 服务?
-
@RuiJarimba,不需要,我也问过。
What is the best way to manage time interval tasks?。抱歉,我更新了问题。
标签: c# .net asp.net-mvc timer scheduled-tasks