在特定时间执行代码 [英] Execute Code at specific time

查看:74
本文介绍了在特定时间执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想在特定时间执行某些功能(例如,每天晚上12.00).所以,我该怎么做?

Hi,


I want to execute some function on specific time.(e.g. Every night 12.00). So, how can I do this??

推荐答案

从您的问题标签开始,我假设您要在ASP.Net中进行此操作.

首先要了解ASP.Net基于请求-响应 [ ^ ]模型.在ASP.Net代码中,很少有实现调度的方法(在指定的时间执行一些代码).

1.在 Global.asax Application_Start 事件中创建并启动 timer 实例.在每个计时器滴答事件中,您要执行的操作.
我不推荐这种方式,因为在ASP.Net中编写计时器功能不是一个好主意.

2.编写计划Windows服务以调用Web服务.
一世.在Web服务中编写要在指定时间执行的代码.
ii.编写Windows服务以在指定时间调用此Web服务.
您可以使用 Quartz.net [ ^ ].
我推荐这种策略

3.如果要在共享环境中(例如在共享主机空间上)托管网站,则可以使用 SQL作业计划定期执行代码.
一世. SQL作业计划程序 [ http://msdn.microsoft.com/en-us/library/ms191439.aspx [ ^ ]

4.否则请考虑一下 Windows Task Scheduler
一世. Windows任务计划程序 [ http://msdn.microsoft.com/zh-CN /library/windows/desktop/aa383614(v=vs.85).aspx [
As from your question tag I assume you want to do this in ASP.Net.

First of all understand that ASP.Net is based on request - response[^] model. There are few ways to implement scheduling(Execute some code at specified time) in ASP.Net code.

1. Create and start timer instance in Global.asax Application_Start event. On each timer tick event do what you want to do.
I do not recommend this way, as it is bad idea to write timer functionality in ASP.Net.

2. Write scheduling windows service to call a web service.
i. Write the code that you want to execute on specified time in a web service.
ii. Write a windows service to call this web service at specified time.
You can use Quartz.net[^] for this purpose.
I recommend this strategy

3. If you are hosting your website in shared environment like on shared hosting space, then you may use SQL Job schedule to execute a code periodically.
i. SQL Job Scheduler[^]
ii. http://msdn.microsoft.com/en-us/library/ms191439.aspx[^]

4. Else give a thought to Windows Task Scheduler
i. Windows Task Scheduler[^]
ii. http://msdn.microsoft.com/en-us/library/windows/desktop/aa383614(v=vs.85).aspx[^]

Hope this helps.
All the best.


创建网页以及您的函数,并创建执行该网页的任务.
create web page along with your function and create task to execute this web page.


^ ]


这篇关于在特定时间执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆