dotNetChris @ Marisic.Net

September 23, 2008

Update: Scheduled Tasks in ASP.NET!

Filed under: Programming — Tags: , , , — dotnetchris @ 4:09 pm

So as I promised I did write my sample project for using this. I’ll include the full files later as the majority of it is just wrappers around classes the files that really do all the magic is the code behind for the default.aspx and the TaskManager.cs file.

public partial class _Default : Page
{
private readonly TaskManager taskManager = new TaskManager();

protected void Page_Load(object sender, EventArgs e)
{
var printTask = taskManager.TaskList.Find(t => t.Id.GetValueOrDefault() == 1);

lblStatus.Text = printTask != null ? (printTask.Active ? "Active" : "InActive") : "Task not found";
}

protected void btnStartTasks_Click(object sender, EventArgs e)
{
taskManager.TaskList.Add(new PrintTask {Active = true, Id = 1, Name = "PrintTask1"});
taskManager.TaskList.Add(new PrintTaskOther {Active = true, Id = 2, Name = "PrintTaskOther2"});
taskManager.TaskList.Add(new PrintTask {Active = true, Id = 3, Name = "PrintTask3"});
taskManager.TaskList.Add(new PrintTask {Active = true, Id = 4, Name = "PrintTask4"});
taskManager.TaskList.Add(new PrintTaskOther {Active = true, Id = 5, Name = "PrintTaskOther5"});
}
}

The real magic that is occuring in here basically your just loading up the task objects into a List object however a few operations are overloaded that change the normal functionality of a List

public class TaskList : IList
{
private readonly List _tasks = new List();
private readonly Cache _cache = HttpRuntime.Cache;
//How long for jobs to wait before waking.
private const double sleepLength = 1;

public List Tasks
{
get
{
if (Count == 0)
{
var enumerator = _cache.GetEnumerator();
do
{
if (enumerator.Current is Task)
_tasks.Add(enumerator.Current as Task);
} while (enumerator.MoveNext());
}
return _tasks;
}
}
public void Add(Task item)
{
var task = _tasks.Find(t => t.Name == item.Name);
if (task == null)
{
_tasks.Add(item);
CacheAdd(item);
}
else
{
task.Id = item.Id;
task.Active = item.Active;
task.LastRan = item.LastRan;

CacheUpdate(item);
}
}

private void CacheUpdate(Task item)
{
_cache.Remove(item.Name);

CacheAdd(item);
}

private void CacheAdd(Task item)
{
if (_cache[item.Name] == null)
{
_cache.Add(item.Name, item, null, DateTime.Now.AddMinutes(sleepLength),
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable,
CacheItemRemovedCallback);
}
}

public CacheItemRemovedCallback CacheItemRemovedCallback { get; set; }

Basically all it does is overrides saving the list solely to the server’s ram and writes it out to the Cache also when objects are added. It also has the Get method overrided so it will read the existing cache objects out by default for the list.

What pulls it all together to make everything work is the

public CacheItemRemovedCallback CacheItemRemovedCallback { get; set; }

This is the method that will get called when a task is removed from the cache’s dictionary. This was set in

public CacheItemRemovedCallback CacheItemRemovedCallback { get; set; }

So whenever a cache item is removed from the stack the event fires off to

public static void TaskExecuting(string key, object value, CacheItemRemovedReason reason)

Which is where we do the real execution of the job. At the end of the job we stick the item back in the cache so it will continue to loop. This is also the spot where you would put in logic to stop the service after so many iterations or it reaches some kind of predefined condition.

Updated: I have moved my source over to Assembla, you can download it directly or setup an SVN connection to view my source. Download Task Scheduler.

BloggingContext.ApplicationInstance.CompleteRequest();

kick it on DotNetKicks.com

About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 242 other followers

%d bloggers like this: