background July 18, 2008

Easy Background Tasks in ASP.NET

As I work on the badge implementation for Stack Overflow, I needed a way to call the code that detects and awards the badges out of band. Traditionally this is done by something like cron or scheduled tasks. I’d rather have the code stay inside our current codebase, though. I asked on Twitter and got…
Avatar for Jeff Atwood
Co-Founder (Former)

As I work on the badge implementation for Stack Overflow, I needed a way to call the code that detects and awards the badges out of band. Traditionally this is done by something like cron or scheduled tasks. I’d rather have the code stay inside our current codebase, though.

I asked on Twitter and got some good responses, everything from “write a service” to “use threads”. I also got a link to Simulate a Windows Service using ASP.NET to run scheduled jobs. Now this is interesting — it’s just simple enough to work:

  1. At startup, add an item to the HttpRuntime.Cache with a fixed expiration.

  2. When cache item expires, do your work, such as WebRequest or what have you.

  3. Re-add the item to the cache with a fixed expiration.

The code is quite simple, really:

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));
}

Works well in my testing; badges are awarded every 60 seconds like clockwork for all users.

Podcast logo The Stack Overflow Podcast is a weekly conversation about working in software development, learning to code, and the art and culture of computer programming.

Related

code-for-a-living July 28, 2021

700,000 lines of code, 20 years, and one developer: How Dwarf Fortress is built

Dwarf Fortress is one of those oddball passion projects that’s broken into Internet consciousness. It’s a free game where you play either an adventurer or a fortress full of dwarves in a randomly generated fantasy world. The simulation runs deep, with new games creating multiple civilizations with histories, mythologies, and artifacts. I reached out to him to see how he’s managed a single, growing codebase over 15+ years, the perils of pathing, and debugging dead cats. Our conversation below has been edited for clarity. 
code-for-a-living July 5, 2021

Best practices for writing code comments

While there are many resources to help programmers write better code—such as books and static analyzers—there are few for writing better comments. While it's easy to measure the quantity of comments in a program, it's hard to measure the quality, and the two are not necessarily correlated. A bad comment is worse than no comment at all. Here are some rules to help you achieve a happy medium.