Mar
9
2010

Fun with .net: Scheduled tasks in asp.net

This is the third in a series of blog posts highlighting interesting snippets hacks I have run across. Although these do work, I do not recommend trying them in production code.

Have you ever been in a situation where you needed a scheduled task to kick off, but you didn’t have access to the server (think shared hosting)? I originally saw this hack on Samir’s blog about a year ago now.

Essentially, you just create a System.Timers.Timer in the Application_Start event of your global.asax, and bam – instant scheduled task! I don’t necessarily recommend this, but I guess if you had no other option it could work.

 

using System.Timers; 

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        Timer timer = new Timer(1000);
        timer.Elapsed += (object s, ElapsedEventArgs args) =>
        {
            LetsDoSomethingAwesome();
        };
        timer.Start();
    }
}

Add comment




biuquote
Loading


AdSense

Software engineer by hobby and trade. When I am not sitting in front of a computer, you can find me playing with my kids. I am lucky enough to be married to my best friend and high school sweetheart. Life couldn't be better!

 

All content is mine, not my employers

Chronology

Tweets

stumped over a design in my app...:(
1 day ago via Silver Bird
RT @ramseyshow: Blake: US adds 243,000 jobs in January and unemployment rate drops to 8.3%, lowest in three years. (AP)
3 days ago via rowi
RT @jnonis: Amen! Walking with ppl does! RT @stephenNcollins I dare say that boycotting a business in the name of Jesus has never won an ...
3 days ago via rowi
@danctheduck wife and I are big tea drinkers though. That or water is about all we have
3 days ago via WindowsLive
@danctheduck lol. We go through 3 pots/day (pot is 30oz) brew at least 3 times or else It gets pricy :) breville tea maker ftw
3 days ago via WindowsLive
@danctheduck how do u brew your TKY? Usually do mine at 195 for 3:30
3 days ago via rowi
@danctheduck wait...now I see it. Stopping by next time I'm in seattle
3 days ago via rowi
Follow me on Twitter