I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering and Programming.
In the first part of this short post series about building a task scheduler system for the ASP.NET I covered some principles as well as the structure of a Task class that represents a particular task.
The other element of the process is of course building a mechanism to schedule this task and start or stop it. This is the topic of the current post. Don’t forget to read the footer notes of this post that have some good news related to this topic.
Before following the post, just let me point that the value of a scheduling system that just depends on the ASP.NET application and works for all the environments and security settings is something obvious. As I pointed in the first post, there are lots of other methods that usually require access to the server resources and depend on other tools but an integrated solution is %100 integrated and simple to use and extend.
The second step in the process is creating a class, say TaskScheduler, that schedules the Task class.
Below code is the simple implementation of this class. It doesn’t have anything except an initialization method that initializes the Task class with the appropriate timer interval and a file path and provides two public methods to start the task or stop it!
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace TaskSchedulerSample
{
public class TaskScheduler
{
#region Fields
Task task = null;
#endregion
#region Public Constructors
public TaskScheduler()
{
Initialize();
}
#endregion
#region Public Methods
public void StartTask()
{
this.task.Start();
}
public void StopTask()
{
this.task.Stop();
}
#endregion
#region Public Constructors
private void Initialize()
{
this.task = new Task(5000);
this.task.FilePath = @"C:\TaskSchedulerSample\file.txt";
}
#endregion
}
}
As you see, the implementation is pretty simple and straightforward but in real world scenarios you need to extend this in order to start and stop a list of tasks based on dynamic configuration. The core idea and mechanism is constant, though!
But the last step to make this work for your ASP.NET application is creating an instance of the TaskSheduler class and call its StartTask and StopTask methods appropriately. Based on your requirements you may choose from different options but the most common way is using the Applicaiton_Start and Application_End events of the Global application class. You can create an instance of the TaskScheduler class in Application_Start event and call the StartTask and also call the StopTask method in Application_End event.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
namespace TaskSchedulerSample
{
public class Global : System.Web.HttpApplication
{
private TaskScheduler _scheduler = new TaskScheduler();
protected void Application_Start(object sender, EventArgs e)
{
this._scheduler.StartTask();
}
protected void Application_End(object sender, EventArgs e)
{
this._scheduler.StopTask();
}
}
}
Now if I start the application and view a page then this task starts running and creates and updates my text file with the current time on a regular basis.

This was a simple implementation of such a task scheduler system in ASP.NET in a practical way. You can download the source code sample for this post series from here.
But after getting Dave’s comment on the first post and noticing the demand for having a general framework for task scheduling in ASP.NET applications, I finally decided to share my own task scheduler system with the community through an open source project. I’ve had a short conversation with Dave and will create a new open source project to release my scheduler publicly with contributions from him to let everyone enjoy using a general mechanism with dynamic configuration like what you see in Community Server. At the moment I’m busy with some stuff but hopefully will do this during the next week and will announce the project here. I also have some plans to contribute this system to some .NET blogging engines like BlogEngine.NET and Subtext.
Mahdi Taghizadeh
Jun 25, 2008 2:36 PM
#
Dear Keyvan,
Thank you so much. A few months ago I searched for an scheduler for ASP.NET but didn't find anything usefull. So I'm eagerly waiting for your project first public release.
Dave Burke
Jun 25, 2008 5:23 PM
#
Keyvan, you are THE MAN!!! My Community Server projects increasingly depend on background tasks, so having an Open Source equivalent will be SO VALUABLE. Thank you so much for spearheading this project (and for Waegis, and the CS MVP Mods, and BlogML, and....)
Reflective Perspective - Chris Alcock » The Morning Brew #123
Jun 26, 2008 2:13 AM
#
Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #123
_valdemar_
Jun 26, 2008 2:17 AM
#
Hi!
What about tasks that have to wait for 12 (or more) hours before next run..? There's great chance an AppDomain will be unloaded by IIS and your tasks wont be executed in time.
One of the ideas how to solve this problem is to create task (~15 min) for regular requesting some page on your webportal (which should prevent unloading)... but I'm sure more elegant way exists :)
Dew Drop - June 26, 2008 | Alvin Ashcraft's Morning Dew
Jun 26, 2008 7:07 AM
#
Pingback from Dew Drop - June 26, 2008 | Alvin Ashcraft's Morning Dew
Dave Burke
Jun 26, 2008 10:20 PM
#
Valdemar, You're absolutely right. Because the ASP.NET task scheduler depends on the App Pool and the site being active, this is not the most mission critical solution for background tasks.
In my experience when I needed to run a task, say, late at night on a company site which is active primarily during business hours, I created a Console app which pinged a web page on my site every 15 minutes so that any ASP.NET-based tasks will perform as needed.
Here's a link to the post if you're interested.
http://tinyurl.com/5kyooz
Regards,
Dave
Everyman Links for June 27, 2008
Jun 28, 2008 6:23 AM
#
Keyvan Nayyeri is one hot Iranian stud! I mean that in a purely non-sexual way, of course. This young
Abidar – ASP.NET Task Scheduling Framework
Jun 30, 2008 12:08 PM
#
Recently I published a couple of blog posts about building a simple ASP.NET task scheduling system in
First Approach to an ASP.NET Task Scheduler
Jul 10, 2008 4:23 PM
#
The subtitle of this post is "Keeping up with Keyvan." The guy is definitely a high octane
Abidar Task Scheduler Application Taking Shape
Jul 19, 2008 5:41 PM
#
Abidar Task Scheduler Application Taking Shape
TaskScheduler « Maycil BloG!
Aug 13, 2008 6:30 AM
#
Pingback from TaskScheduler « Maycil BloG!
My Best Blog Posts in 2008
Dec 31, 2008 1:40 PM
#
In the past 3.5 years of blogging, I haven’t had such best pick up collections in the end of the year, but now that everybody is writing one, why shouldn’t I write my own?! Collecting this list, I could realize some interesting facts that completely changed
kareem hassan
Dec 04, 2009 3:25 PM
#
source file not found.
could you please send the files to my email please,
your article is amazing and this will help in my project.
thank you
Mahesh
Dec 20, 2009 11:09 PM
#
Can you please give me its code
Thanks,
Bipul
Dec 21, 2009 12:29 PM
#
syed Haider
Jan 25, 2010 4:38 PM
#
Leave a Comment