Often there are times when you need a task to be executed automatically, after a fixed period of time, similar to JOBS in SQL Server. Even though implementing JOBS in SQL Server isn't as tough as nails but they won't be helpful if the functionality of JOBS are to be implement ASP.NET. So, here is a short and crispy implementation that would definitely help you implement the functionality of performing a series of code on a daily basis or after a fixed interval of time. For example, sending emails to the customers whose subscription will soon expire, or an application that reminds you of a specific task after every 2 or 3 days.
The implementation is not an issue but before starting the implementation what we need is a utility that would run indefinitely and that will help us invoke the method that needs to be executed automatically. Unfortunately there is no such utility in ASP.NET that would help us in getting the desired behaviour so the only option here is to catch hold of a simple but a good work around. Normally the operations done on an ASP page is a reflex to an action like "Click of a Button" or "Loading of a Page", so these won't be of any help since the functionality that needs to be implemented is to receive a reflex action without any input action. So we need something that would provide us an output without any input.
Something like a Cache item can be helpful in this case and a call-back from this item can easily be received.
The Implementation
All the operation that we need to apply on the cache item will be implemented in the Global.asax of the ASP.NET application.
The ASP worker process checks the cache item every once in a while. So the first thing we need to do is register a cache item and set the duration for expiry of the call-back.
- private const string SampleCacheKey = "CacheItem";
- protected void Application_Start(Object sender, EventArgs e)
- {
- RegisterCacheItem();
- }
- private bool RegisterCacheItem ()
- {
- if( null != HttpContext.Current.Cache[SampleCacheKey] ) return false;
- HttpContext.Current.Cache.Add(SampleCacheKey, "Test", null,
- DateTime.MaxValue, TimeSpan.FromMinutes(10),
- CacheItemPriority.Normal,
- new CacheItemCallback( CacheItemCallback ) );
- return true;
- }
- public void CacheItemCallback( string key,
- object value, CacheItemRemoved remove)
- {
- CallWebsite();
- PerformTask();
- }
- private const string website =
- "http://localhost/SendEmail.aspx";
- private void CallWebsite ()
- {
- WebClient client = new WebClient();
- client.DownloadData(website);
- }
- protected void Application_BeginRequest(Object sender, EventArgs e)
- {
- //Register another item
- if( HttpContext.Current.Request.Url.ToString() == website)
- {
- // Add item
- RegisterCacheItem();
- }
- }
Perform the Task (send email):
The Aftermath
- private void PerformTask()
- {
- MailMessage message = new MailMessage();
- message.To.Add(new MailAddress([email protected]));
- message.Subject = "Test Mail";
- message.Body = "This is a Test Mail";
- message.From = new MailAddress("[email protected]");
- SmtpClient mclient = new SmtpClient();
- mclient.Host = "smtp.gmail.com";
- mclient.Port = 587;
- mclient.EnableSsl = true;
- mclient.Credentials = new System.Net.NetworkCredential("[email protected] ", "test@123");
- mclient.Send(message);
- }
Congrats! You have successfully implemented the functionality to invoke a method automatically over a fixed period of time. This implementation is one of the easiest methods to perform scheduled tasks would also stand by you even when the WebPage is not running, but provided it is hosted.

Code AlonePosted Feb 5, 2019, 4:29 AM
Kindly share the sample Code Ankita Kulkarni
Vignesh ManiPosted Jun 26, 2015, 6:06 AM
Nice One
Karthik Muthu KaruppanPosted Apr 23, 2015, 11:28 AM
good one
vineeth sPosted Apr 20, 2015, 2:36 AM
ohhh reallly helpful
Rahul BansalPosted Apr 20, 2015, 12:13 AM
Nice start :)
Ankita KulkarniPosted Apr 19, 2015, 3:53 PM
Thank you everyone :)
Santhakumar MunuswamyPosted Apr 19, 2015, 3:49 PM
Welcome Ankita!
Santhakumar MunuswamyPosted Apr 19, 2015, 3:49 PM
Good Start
Khargesh RajputPosted Apr 19, 2015, 10:48 AM
Thanks for sharing
Manoj BhoirPosted Apr 19, 2015, 8:09 AM
Good one. Thanks for sharing!
Manoj KulkarniPosted Apr 19, 2015, 6:37 AM
Good One
NitinPosted Apr 19, 2015, 5:40 AM
good one
Aditya KumarPosted Apr 19, 2015, 5:30 AM
Nice Ankita! :) Thanks for sharing, will definitely help many. :)
Praveen KumarPosted Apr 19, 2015, 5:27 AM
Nice Ankita! We always came across to implement this functionality by using SQL job but now I can do it by ASP.NET as well. Thanks for sharing!