C# - 任务调度
我正在使用 Asp.Net - Web Forms - Framework 3.5
我想在午夜 12 点向网站用户发送电子邮件。我已经制作了一个发送电子邮件的类方法,但是在午夜 12 点自动执行它的最佳方法是什么?
在 Linux 中我们有 cron 作业,那么 Windows 上的专用服务器和共享服务器上有什么?
I am using Asp.Net - Web Forms - Framework 3.5
I want to send emails to web-site users at 12am mid night. I have made a class method that sends emails but what is best way to auto execute it at 12 mid night?
In linux we have cron jobs, what's on windows both dedicated and shared server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最简单的方法是编写一个发送电子邮件的小命令行应用程序,然后使用 Windows任务计划每天午夜运行应用程序。
Simplest way is to write a little command line app that sends emails, then use Windows Task Scheduling to run the app every day at midnight.
作为
cron
的替代品,您可以尝试at
命令并运行一些可执行文件来发送电子邮件,或者您可以创建一个 Windows 服务来完成这项工作。As a
cron
substitute you can tryat
command and run some executable which will send e-mails, or you can create a windows service to do this job.当在共享主机上时,无法访问任务调度程序,我经常将一些代码放入 Global.asax 中,这将在应用程序的 OnStart 中创建一个新的静态 Timer 对象。然后,当计时器到期时,我会每隔一小时左右运行我的类方法。
这样做的缺点是,您必须保持应用程序池 24/7 运行,否则它将停止运行,您的代码将无法每小时运行。
另一种方法是创建一个页面,运行类方法中的代码,并在每晚午夜自动从外部源触发该页面。您只需设置一个 wget/curl 脚本即可以编程方式访问该 URL。
最后,您还有其他解决方案,如另一篇文章中详述:
你可以吗运行一个从 ASP.Net 项目运行计划任务的“服务”?
When on a shared host, with no access to the task scheduler, I've often times put some code in the Global.asax, which would create a new static Timer object in the OnStart of the application. I would then run my class method every hour or so, when the Timer elapsed.
The downside to this, is that you have to keep your application pool running 24/7, otherwise it will spin down and your code won't run hourly.
An alternative, would be to make a page that runs the code from your class method, and trigger that page from an external source automatically at midnight each night. You could just setup a wget/curl script to hit that URL programmaticly.
Lastly, you have other solutions, as detailed in this other post:
Can you run a "service" that runs a scheduled task from an ASP.Net project?
编写一个 Windows 服务定期调用代码来发送电子邮件就可以完成这项工作。
Writing a windows service calling the code periodically for sending emails would do the job.
您可以使用任务计划程序,它相当于 winnows 的 chron,或者您可以编写一个 Windows 服务来定期检查时间,如果是在午夜之后,它可以发送电子邮件。
您无法从 Web 表单执行此操作,因为该代码仅在用户访问网站时执行,而让线程保持运行是非常糟糕的做法;无论如何,iis 可能会回收它。
You can use task scheduler which is the winnows equivalent of chron, or you can write a windows service that periodically checks the time and if it's after midnight, it can send emails.
You can't do this from your web form because that code only executes when a user is on the site, and it's very bad practice to leave a thread running; iis would probably recycle it anyway.
我使用 Quartz.NET 取得了一些成功,但其他人建议的任务调度程序和简单的命令行应用程序组合是最简单的方法。
I've had some success using Quartz.NET, but the task scheduler and simple command line app combination as suggested by others is the simplest way to do it.
在 Windows 服务中创建应用程序,并在 app.config 文件中使用计时器并将计时器设置为午夜 12 点。
Create your application in windows service and in app.config file use timer and set the timer to 12 midnight.
如果您需要在共享托管环境中运行计划任务/cron 作业,您可以在此处找到免费的 Web cron 作业提供商列表:
ASP.NET:自由安排任务(Cron工作)
If you need to run a scheduled task/cron job in a shared hosting environment, you'll find a list of free web cron job providers here:
ASP.NET: Free scheduling of your tasks (Cron jobs)