Warning this post is very techy and boring. Do not read unless your a programmer.
I wanted to create a scheduled task (in a separate thread) to run every couple of hours on an ecommerce site, and could not find a good solution. After much googleing and piecing some things together I figured it out and it is really easy to do this (See code at bottom of post). There are great articles on creating/using threads in asp.net, so I don't feel a need to cover it here (just make sure to handle all of your errors in a thread or it will fail w/o throwing an error in the main application which can be confusing).
My day job includes building/maintaining the websites for Velowear.com. Velowear.com uses a overblown order management system called Ecometry to maintain products and everything else to do with running a direct retail business. The web interface to Ecometry,webcom.dll a com object, is a very archaic and non object oriented approach to getting at this data. One of the main problems is that whenever our website needed to query data from Ecometry it has to do a round trip over a T1 connection between our web host and our facility, which isn't very efficient. Plus when you want to merchandise your product a certain way (i.e. display what colors are available on category search) I realized that we needed some kind of product caching system. Luckily I had pretty much created my own objects that represented our products as they fit our business, now the challenge was creating an updated list of these product objects.
The first step in this process was creating a separate thread that would query all of our products in Ecometry. This process takes ~ 30 minutes when running on our web server and talking to our App server (at a different location). The product objects have a timeout feature which allows them to refresh themself after a certain amount of time has passed or if their inventory level is low to make sure the data is always fresh. The cache is first written out to a binary file on the web server and then loaded into memory, then if the web server restarts it can grab the cache file and keep responding to request w/o having to wait 30 minutes to finish querying data from Ecometry.
This caching has been working well for the past month and a half, but there was still an important component missing, what happens when you add new product, but they are not in the cache? Well technically you can visit the product page and it will look for the product in the cache and then try to query Ecometry for the data, if the data is found, then it is added to the in memory cache. The problem is when you want 25 new products to 'go live' this is not a great option.
What is so cool about ASP.net is that I can schedule this cache refresh process to happen every 4 hours. This in turn keeps my binary cache file almost in real time with our real product database, which is super cool.
Below is the example code (in C#) for creating a scheduled event (place this code in your global.asax file):
void Application_Start(object sender, EventArgs e)
//4 hour Timer
System.Timers.Timer tm = new System.Timers.Timer(4 * 360 * 1000);
tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
//THIS CODE WILL RUN EVERY FOUR HOURS
//Code here to re-get cache file