在托管环境中计划执行 .NET 代码?
我在控制台应用程序中有大约 40 行 .NET 代码,用于读取 RSS 源并将信息存储在数据库中。只要 RSS 提要存在(无限期地),我就需要每晚执行此代码。目前,我只是启动控制台应用程序。从我的家用电脑。
因为我不能相信自己每天晚上都记得执行此操作,所以我需要以某种方式托管此代码。我想以某种方式拥有这个应用程序。或代码(它可以轻松地放入 ASP.NET 页面代码隐藏中并在页面加载时触发执行)自动运行,而无需我运行控制台应用程序。手动。
有什么想法吗?
编辑:我不想从我的计算机运行此代码;我不能保证我的电脑不会每晚都休眠或连接互联网。
编辑:现在我正在考虑在global.asax的Application_Start中使用BackgroundWorker生成一个后台线程,让它每天下载一次RSS feed,并在其余时间使用Thread.Sleep()。
I've got about 40 lines of .NET code in a console application that read and RSS feed and store information in a database. I need this code to execute every night for as long as the RSS feed exists (indefinitely). Currently, I just launch the console app. from my home computer.
Because I can't trust myself to remember to do this every night, I somehow need to have this code hosted. I'd like to somehow have this app. or code (it could easily be put in an ASP.NET page codebehind and triggered to execute when the page loads) run automatically without me having to run the console app. manually.
Any ideas?
EDIT: I don't want to run this code from my computer; I can't guarantee my computer will not be hibernating or connected to the Internet every night.
EDIT: Right now I'm thinking spawn a background thread using BackgroundWorker in Application_Start of the global.asax, have it download the RSS feed once a day, and Thread.Sleep() the rest of the time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管存在争议,Jeff Atwood 在博客中介绍了他们如何使用缓存过期来实现这一目标。请访问 https://blog.stackoverflow.com/2008/07/easy -aspnet 中的后台任务/。
您可以使用类似的方法并安排缓存在您希望进程启动时到期。
Although controversial, Jeff Atwood blogged about how they accomplished this for SO using cache expiration. Check it out at https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/.
You could use a similar method and schedule the cache to expire at the time you want the process to kick off.
您可以使用 Windows 内置任务计划程序来启动控制台应用程序。
You could use the Windows built-in task scheduler to start the console application.
我建议只使用“at”命令,而不是将这个小应用程序托管在某个地方。
I would suggest just using the 'at' command instead of getting this small app hosted somewhere.
向您的虚拟主机发送电子邮件。一些托管主机会为您设置计划任务。
他们可能对如何调用计划任务有不同的策略(即有些可能要求它是 aspx 页面而不是 EXE)
Email your webhost. Some managed hosts will set up a scheduled task for you.
They will likely have different policies regarding how to call a scheduled task (i.e. some may require it to be an aspx page and not an EXE)
您是否看过Quartz Scheduler for .NET?
引用口号:Quartz.NET 是一个功能齐全的开源作业调度系统,可用于从最小的应用程序到大型企业系统。
Have you looked at Quartz Scheduler for .NET?
Quoth the tagline: Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.