5,276,406 members and growing! (17,084 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Schedule your web tasks with WebTaskScheduler

By SalarSoft

This tool provides easy web task scheduling, designed for ASP.NET and using Caching technique.
C# (C# 2.0, C#), VB 8.0, VB, Windows, .NET (.NET, .NET 2.0, Mono), ASP.NET

Posted: 23 Mar 2008
Updated: 23 Mar 2008
Views: 4,975
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 3.50 Rating: 4.50 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
2 votes, 33.3%
4
3 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

WebTaskScheduler user guide in ASP.NET

Read WebTaskScheduler user guide in ASP.NET in my site

Introduction

Desktop applications is very different form web applications in task scheduling. The problem of web applications is lack of a process to schedule their tasks. ASP.NET provides some technologies one of which I used to create task scheduler for web applications. I have used Caching techniques.

Easy Use

Please follow these steps:

  1. Add a reference to SalarSoft.WebTaskScheduler.dll assembly in your project.
  2. A callback method will be needed, so write it like following example:
C#

static void MyTask_CallBack(WebTaskEventArgs e)
{
 // Enter your codes here
}

VB.NET

Shared Sub MyTask_Callback(ByVal e As WebTaskEventArgs)
 ' Enter your codes here
End Sub
This callback method has an argument of WebTaskEventArgs type. This method will be called when the task is executed.
  1. To run a task you need to add it. The following example adds a task that will run every weekend.
C#

WebTaskScheduler.Add("MyTask", MyTask_CallBack, TaskExecutePeriod.Weekly);
In VB.NET the codes are a bit longer. Here is a VB.NET example:
VB.NET

Sub AddMyTask()

 'Line 1
 Dim onMytaskCallBack As WebTaskExecuteCallback

 'Line 2
 onMytaskCallBack = New WebTaskExecuteCallback(AddressOf MyTask_CallBack)

 'Line 3
 WebTaskScheduler.Add("MyTask", onMytaskCallBack, TaskExecutePeriod.Weekly)
End Sub
On the first line we defined a variable of WebTaskExecuteCallback type. On the second line we created a new instance of WebTaskExecuteCallback type to the variable, and on the third line we added the task to the class.
  1. Please apply steps 2 and 3 in the Global.asax file in ApplicationStart event. That's it.

Features

  • How to add a new task with ADD method

Add method is used to add a new task. Here are three ways it is defined:

Method 1:
WebTaskScheduler.Add( String , WebTaskExecuteCallback , Integer  )


Method 2:
WebTaskScheduler.Add( String , WebTaskExecuteCallback , TaskExecutePeriod )


Method 3:
WebTaskScheduler.Add( String , WebTaskExecuteCallback , TimeSpan )

The two first arguments of these overloads have same behavior.

The first argument "key" which its type is string: Specifies the name of the task. This name should be unique. This key may be used for future operations.

The second argument "callback" which its type is WebTaskExecuteCallback: Specifies the callback method. This method will run if the task is executed. The method with WebTaskExecuteCallback type has a WebTaskEventArgs type of argument which has been described at the bottom of the page.

The third argument of Method 1, "DaysPeriod", which its type is integer: Specifies intervals between the execution of the tasks. If it is set to 10, the task will run after 10 days.

The third argument of Method 2, "period", which its type is TaskExecutePeriod: Specifies the type of the task execution intervals. The value can be one of None , EveryNoon , Daily , Weekly , TwoWeekly or Monthly. For example if the value sets to Weekly, the task will run every weekends.

The third argument of Method 3, "customPeriod", which its type is TimeSpan: Through this, the task will run after the specified time. For example if the value is set to 45 minutes, the task will run after 45 minutes of adding the method call.

  • Deleting a task

The Remove method can be used to remove a task. Here is its definition:

Method 1:
Remove(string key)

Method 2:
Remove(WebTaskItem)

The first method accepts the key of the task. The second method accepts the task item which its type is "WebTaskItem".

  • Getting Details of a Task

The GetItem method returns the existing task item.

GetItem( string )
Returns WebTaskItem

This method has an argument that is the key of the task which is defined in Add method. This method will return WebTaskItem.

  • Synchronous Access

There are two methods to lock and unlock the access to WebTaskScheduler instance.
This is an advanced option to prevent threads from multiple access to methods. With these methods only one thread can have access to the class and the other threads will wait for the current thread to finish.

Here are theirs the definition:

Method 1:
Lock()

Method 2:
Lock( wait )

Method 3:
Unlock()

Call Lock method to lock the access, and call Unlock method to unlock the access.
The second method has an argument that specifies Wait option to check if there is another thread that uses the instance; if the value is set to false and there is another thread working with the class, the method will throw an exception.

Important: Please use try..finally block when you're going to use these methods.

Here is an example:

C#:

WebTaskScheduler.Lock();
try
{
    WebTaskScheduler.Add("MyTask", MyTask_CallBack, 1);
}
finally
{
    WebTaskScheduler.Unlock();
}

VB.NET:

WebTaskScheduler.Lock()
Try
 Dim onMytaskCallBack As WebTaskExecuteCallback
 onMytaskCallBack = New WebTaskExecuteCallback(AddressOf MyTask_CallBack)
 WebTaskScheduler.Add("MyTask", onMytaskCallBack, TaskExecutePeriod.Weekly)
Finally
 WebTaskScheduler.Unlock()
End Try
  • TaskExecutePeriod
Name Decription
None Executes the task immediately (not recommended)
EveryNoon Executes the task every noon
Daily Executes the task every night
Weekly Executes the task every weekend at night
TwoWeekly Executes the task every two weeks at night
Monthly Executes the task every month at night
ShamsiWeekly Executes the task every week at night using Persian dates
ShamsiTwoWeekly Executes the task every two weeks at night using Persian dates
ShamsiMonthly Executes the task every month at night using Persian dates
  • WebTaskEventArgs
This class has two properties: "TaskItem" and "CanContinue".
CanContinue specifies whether the task can run again next time or not. If it is set to false the task will be removed after execution. The default value is true.
TaskItem specifies the task item which its type is WebTaskItem. This can be modified in callback.

Some tips

  1. Important: In your ASP.NET site, always add your tasks in Application_Start event of Global.asax.
  2. The added tasks are not stored on the disk, so they will be removed if the web application shuts down or restarts.
  3. The WebTaskScheduler is accessible from everywhere in your site. Just add the assembly reference to your application!
That's all!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SalarSoft



Occupation: Web Developer
Location: Anonymous Proxy Anonymous Proxy

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Subject  Author Date 
Generalinteresting.memberJason Witty14:12 3 Apr '08  
GeneralRe: interesting.memberSalarSoft22:06 16 Apr '08  
QuestionWhat happens when no one is visiting?memberThomas Eyde4:05 3 Apr '08  
GeneralRe: What happens when no one is visiting?memberSalarSoft22:03 16 Apr '08  
GeneralRe: What happens when no one is visiting?memberThomas Eyde0:38 17 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2008
Editor:
Copyright 2008 by SalarSoft
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project