First Approach to an ASP.NET Task Scheduler

The subtitle of this post is “Keeping up with Keyvan.” The guy is definitely a high octane .NET machine. I told him how excited I was about his work on an ASP.NET Task Scheduler and as a result have been made an honorary participant in The Abidar Project (Keyvan comes up with the best names.) You’ll find Abidar in its inception phase on CodePlex. So that it’s clear, this is Keyvan’s project and I am along for the ride.

As you might infer from my Services page, I’m working on a framework to extend and customize Open Source Community Applications. Creating background tasks would be an essential component of that framework, just as it is an important element in Community Server development.

Keyvan’s energy inspired me to begin approaching an ASP.NET Task Scheduler for use in the real world. With the post of Keyvan’s Task Scheduler, Part II I was able to take his code and within a few minutes have a functional background task running on Application_Start().

I’ve been studying Open Source development lately and one of the startup rules of an Open Source Project is to scour the net to see if what you want to do has already been done. While I found no ASP.NET Task Scheduler projects per se, I did find this excellent 2003 Angry Coder post by Michael Teper.

To take Keyvan’s source to the next phase would require loading multiple tasks described in a database table or configuration file. Michael’s post provided the ingredients for the next course. (I’ve been watching too much of the Food Network lately…)

Using Michael’s logic, instead of starting a specific task in the Global.asax like so,

void Application_Start(object sender, EventArgs e)
{
someTask.StartTask();
}

we employee a SchedulerConfiguration class specifying the sleep interval (for now) and a list of tasks. Now we can load those tasks from some other source and pass them to a Scheduler class that executes the individual tasks in the Configuration file. The logic would be

SchedulerConfiguration config = new SchedulerConfiguration([sleepInterval]);
foreach (task from config file or other source)
{
string _typeString = “Some.Class, SomeNamespace”;
Type _type = Type.GetType(_typeString);
ISchedulerTask task = Activator.CreateInstance(_type) as ISchedulerTask;
config.Tasks.Add(task);
}
Scheduler scheduler = new Scheduler(config);
System.Threading.ThreadStart _threadStart = new System.Threading.ThreadStart(scheduler.Start);
schedulerThread = new System.Threading.Thread(_threadStart);
schedulerThread.Start();

The Sleep Interval needs to be task-specific, of course, which is where Keyvan’s source code comes in. We would also want to add the interval and other properties Keyvan listed in his code to a task base class – IsRunning, LastRunTime, and so on.

The two hours or so on a Sunday night spent on the above proves that an Open Source ASP.NET Task Scheduler is definitely doable. So as Keyvan said at the bottom of his Abidar announcement, Stay tuned!

Article written by

A long time developer, I was an early adopter of Linux in the mid-90's for a few years until I entered corporate environments and worked with Microsoft technologies like ASP, then .NET. In 2008 I released Sueetie, an Online Community Platform built in .NET. In late 2012 I returned to my Linux roots and locked in on Java development. Much of my work is available on GitHub.