ASP.NET/C#:以特定间隔运行命令 [英] ASP.NET / C#: Run command at specific interval
问题描述
我有一个使用SinglarR和MVC5编码的拍卖网站.我在数据库中存储了一个针对每个竞标者的信用卡令牌.在拍卖结束时,需要向中标者的信用卡收费.
I have an auction site coded using SinglarR, and MVC5.I have a credit card token stored in my database for every single bidder. At the end of the auction, the winning bidder's credit card needs to be charged.
最有效的方法是什么?我考虑过要使用过滤器,但是,代码只会在访客下次到达时才运行,这在某些日子可能会很长一段时间.
What would be the most efficient way to accomplish this? I thought about using a filter, however, the code would only be ran the next time a visitor arrives, which might be a long period of time on some days
推荐答案
听起来像您需要预定任务.可以使用HttpModule完成.实施类似的东西:
Sounds like you need a scheduled task. It can be done with HttpModule. Implement something like:
public class CheckAuctionsScheduleModule : IHttpModule
{
static Timer timer;
long interval = 60000; //60 secs
static object synclock = new object();
public void Init(HttpApplication app)
{
if(timer==null) timer = new Timer(new TimerCallback(CheckAuctions), null, 0, interval);
}
private void CheckAuctions(object obj)
{
lock (synclock)
{
//implement here your logic
//check completed auctions
//send notifications to bidder etc.
}
}
public void Dispose()
{
//implement if needed
}
}
在您的web.config中:
In your web.config:
<system.webServer>
<modules>
<add name="CheckAuctionsSchedule" type="MyMvcApp.Modules.CheckAuctionsScheduleModule"/>
</modules>
</system.webServer>
注意:只要MVC应用程序正在运行,模块就可以工作.还有其他计划任务的解决方案(如Microsoft建议的Win服务).这很有用.
Note: Module works as long as MVC application running. There are other solutions for scheduled tasks(as microsoft recommends win services). This can be useful.
这篇关于ASP.NET/C#:以特定间隔运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!