Fun with .net: Scheduled tasks in asp.net

9. March 2010

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

short link to this post:

asp.net, Fun with .net , ,

Comments are closed