Wednesday, April 13, 2011

How do I create a server side timed event?

I'm working a a project for a new web app. I'm identifying the key points of functionality, and one thing that I clearly don't know how to approach is as follows:

Once per minute the server needs to run a series of calcs on its data, and post new derived summaries.

So in essence what I need is a way to run a server side script once per minute, reliably, even when users aren't active.

I'm using ASP.NET 3.5, C# for the web interface. Using SQL 2005 for the data back end.

I suppose all the data thats being changed is SQL side, so the script could function completely there if thats more workable.

Thanks for any suggestions.

From stackoverflow
  • nothing cronjob like on win machines?

  • Place an item in the cache with a minutes expiry. Make sure you assign it a call back method for when the item is dropped. After a minute the item is removed your call back runs. Do you stuff there then add the item to the cache again.

  • There are two options for "background" processing on a web site. You could not do it as part of the site, but rather do it in a windows service (can use the same business objects an everything) or you can do it in a background thread started in the web app. We've used both with success, different ones in different places. In our Application_Start we do:

    //Submit background thread to update time left hospital
    System.Threading.Thread checkoutThread = 
      new System.Threading.Thread(BackgroundThread.CheckoutUpdate);
    checkoutThread.IsBackground = true;
    checkoutThread.Priority = System.Threading.ThreadPriority.BelowNormal;
    checkoutThread.Start();
    

    Where BackgroundThread.CheckoutUpdate is defined as public static void CheckoutUpdate()

  • Get Sql Server to do it all. Create a stored procedure to perform the operation, and set up a job within Enterprise Manager -->Management--> Jobs. Tell the job to call the stored procedure.

  • Can't you just use System.Timers?

    using System;
    using System.Timers;
    namespace App
    {
        class Program
        {
            static void TimerEvent(object s,ElpasedEventArgs arg)
            {
                //handle your stuff here
            }
            static void Main()
            {
                Timer t = new Timer(60000); //60 seconds * 1000 = 60.000
                t.Elapsed += new ElapsedEventHandler(TimerEvent);
                t.Start();
            }
        }
    }
    

    just in case you didn't know..

0 comments:

Post a Comment