How to do scheduled tasks in ASP.NET?

58 views Asked by At

I am coding a text based web browser game in ASP.NET. But i need a lil bit info so i decided to ask here. When user enter any quest, lets say that quest take 10 mins to proceed. If user exits from game, how can i make my script to run automaticly and finish the quests and upgrade players power etc? I heard something like CronJob. But i dont know if it works for ASP.NET so i wanna hear any idea before i do this. Thank you for your help.

1

There are 1 answers

5
salgiza On BEST ANSWER

You could just add a cache item with a callback function.

public void SomeMethod() {
    var onRemove = new CacheItemRemovedCallback(this.RemovedCallback);   
    Cache.Add("UserId_QuestId", "AnyValueYouMightNeed", null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
}

public void RemovedCallback(String key, Object value, CacheItemRemovedReason r){
   // Your code here
}

The cache item has an expiration of 10 minutes, and as soon as it is removed from memory the RemovedCallback will be invoked.


Note: just to completely answer your question:

  • You could also use some of the frameworks available to schedule tasks in asp.net (such as Quartz.net).
  • Create a Console project in your solution and schedule it on the Web server (using the Windows Scheduler).
  • Create a Web Job project, if you are deploying your web in Azure.

But in your situation, using the cache is probably the simplest solution.