简体   繁体   English

ASP.NET / C#:以特定间隔运行命令

[英]ASP.NET / C#: Run command at specific interval

I have an auction site coded using SinglarR, and MVC5.I have a credit card token stored in my database for every single bidder. 我有一个使用SinglarR和MVC5编码的拍卖网站。我在数据库中存储了针对每个竞标者的信用卡令牌。 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 我考虑过要使用过滤器,但是,代码只会在下次访问者到达时才运行,这有时可能会花费很长时间

Sounds like you need a scheduled task. 听起来您需要安排任务。 It can be done with HttpModule. 可以使用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
    }
}

In your web.config: 在您的web.config中:

<system.webServer>
  <modules>
    <add name="CheckAuctionsSchedule" type="MyMvcApp.Modules.CheckAuctionsScheduleModule"/>
  </modules>
</system.webServer>

Note: Module works as long as MVC application running. 注意:只要MVC应用程序运行,模块就可以工作。 There are other solutions for scheduled tasks(as microsoft recommends win services). 对于计划的任务,还有其他解决方案(如Microsoft建议的Win服务)。 This can be useful. 可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM