Given an ASP.NET application, I need to run a maintenance process on a regular basis (daily, hourly, etc.).
What's the best way to accomplish this without relying on an external process like a scheduled task on the server (assume I don't have access to the server - shared hosting environment).
Here's the way that StackOverflow does it:
private static CacheItemRemovedCallback OnCacheRemove = null;
protected void Application_Start(object sender, EventArgs e)
{
AddTask("DoStuff", 60);
}
private void AddTask(string name, int seconds)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, seconds, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
// do stuff here if it matches our taskname, like WebRequest
// re-add our task so it recurs
AddTask(k, Convert.ToInt32(v));
}
Details: [https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/][1] [1]: https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With