0

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.

phoeNix
  • 63
  • 1
  • 8

1 Answers1

2

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.

salgiza
  • 5,832
  • 2
  • 26
  • 32
  • Thank you for your answer. If we use this callback function, will it invoke even if user is not on our website? – phoeNix Nov 12 '14 at 15:49
  • Correct. In fact, you won't have access to the user's Session, even if he/she were still connected. That's why you should add to the cache an object (the second param) with whatever values/keys you need to execute the scheduled task. – salgiza Nov 12 '14 at 15:53
  • @phoeNix - this will execute, even if the user has left the site. Note that you can't access Session and that the Cache is shared amongst all users, so you need a user-specific key. – Hans Kesting Nov 12 '14 at 15:53
  • Thank you very much. You're both very helpful! By the way, as i am doing a game project, if i have many players, will that cache thing slow my game? Should i go PHP or ASP.NET is suitable for it? – phoeNix Nov 12 '14 at 15:59
  • It should not slow your game at all. You must remember not to store too much info in it (some ids/strings, but don't add an object with loads of data) to keep memory usage low, but that's about it. And regarding your last question, yes ASP.NET (remember to use MVC) is certainly suitable for it! :) – salgiza Nov 12 '14 at 16:14