Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Scheduled Tasks in ASP.NETVery often, there is a need to execute some code on certain date and time, or to repeat execution in regular time intervals. In case of ASP.NET web application, that could be sending of reminder e-mails, analyze traffic data, creating reports, maintenance tasks etc. ASP.NET doesn't provide some straightforward way to schedule tasks. Also, only what HTTP protocol can do is to return some output as a response after receives web request. Fortunately, there are numerous solutions to solve this problem. Scheduled tasks methods could be divided in two groups: - Pure ASP.NET methods, like using of timer, cache expiration or threads, In this tutorial, I will show three ways to simulate scheduled tasks using ASP.NET only. To learn more about options for scheduled tasks in ASP.NET using external application see Task Scheduler or Windows service in ASP.NET tutorial. ASP.NET only methods are often an option if you have shared hosting, but they have some drawbacks. Scheduled tasks using threadingSolution that uses threads is very simple. We'll use Application_Start procedure in Global.asax to initially start task. Then, in separate thread we'll call task again in regular time intervals. Code in Global.asax could look like this: [ C# ] <%@ Application Language="C#" %> [ VB.NET ] <%@ Application Language="VB" %> Scheduled tasks using timersVery simple solution to perform scheduled tasks is by using timer. Again, we'll use Global.asax and Application_Start procedure. In Application_Start we'll create a Timer and use Elapsed event to execute task in regular time intervals. To make it so, write this code in Global.asax file: [ C# ] <%@ Application Language="C#" %> [ VB.NET ] <%@ Application Language="VB" %> This solution looks very simple and neat, at least theoretically. In practice, timer is often unstable when used in web application. It is common that timer stops after 20-30 minutes, especially on shared hosting where hosting provider tries to save resources and recycle web application. Scheduled tasks using cache expiration as a triggerASP.NET Cache expiration is one more method you can use to create scheduled tasks. In this method, it is not important what you put in the cache. In this example I added short string "1", but could be anything. Useful for scheduled tasks is the fact that cache expires after specified time interval and then executes selected function (scheduled task). Here is example of Global.asax file that uses cache expiration to schedule tasks in ASP.NET application. First, Application_Start method calls ScheduleTask procedure that uses cache expiration to schedule when task will be executed. In this example, cache willl expire after one hour. Then, after cache expired, SetTimer() is called. SetTimer function calls DoTask() method which represents code of scheduled task, and also calls again ScheduleTask() function to plan next task execution. [ C# ] <%@ Application Language="C#" %> [ VB.NET ] <%@ Application Language="VB" %> ConclusionScheduled task could be running of Windows application, script, web page, sending email etc. The problem with pure ASP.NET methods could be insufficient rights, since ASP.NET by default runs as NETWORK SERVICE or ASPNET account which are very limited. Also, ASP.NET application could restart or even stop work because of numerous reasons. If ASP.NET application stops, scheduled tasks are not executed. If there is nobody on website (no web requests), server could stop application after short time. The possible workaround could be to keep web application alive using scheduled task that will use WebClient class to periodically (e.g. every ten minutes) make web requests to some page. This doesn't help if ASP.NET application restarts. If task is time critical, consider more reliable options that use external application in Task Scheduler or Windows service in ASP.NET tutorial. Using of external application is also recommended if you have long and heavy tasks that could hurt web application's performances. If visitors' experience will be bad because of some scheduled task, remove it from web application. In case that you are not able to use Windows service or Task Scheduler because your website is hosted on shared hosting, try to split long and heavy task into multiple short steps. Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | Google |