Click here to Skip to main content
13,409,230 members (42,571 online)
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 23 Mar 2008

Schedule your web tasks with the WebTaskScheduler

, 23 Mar 2008
Rate this:
Please Sign up or sign in to vote.
This tool provides easy web task scheduling, designed for ASP.NET and uses Caching.


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

Easy to Use

Please follow these steps:

  1. Add a reference to the SalarSoft.WebTaskScheduler.dll assembly in your project.
  2. A callback method will be needed, so write it like the following example:
  3. //C#
    static void MyTask_CallBack(WebTaskEventArgs e)
        // Enter your codes here


    Shared Sub MyTask_Callback(ByVal e As WebTaskEventArgs)
     ' Enter your codes here
    End Sub

    This callback method has an argument of the WebTaskEventArgs type. This method will be called when the task is executed.

  4. To run a task, you need to add it. The following example adds a task that will run every weekend:
  5. //C#
    WebTaskScheduler.Add("MyTask", MyTask_CallBack, TaskExecutePeriod.Weekly);

    In VB.NET, the code is a bit longer. Here is the VB.NET example:

    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 define a variable of type WebTaskExecuteCallback. On the second line, we create a new instance of the WebTaskExecuteCallback type, and on the third line, we add the task to the class.

  6. Please apply steps 2 and 3 in the Global.asax file in the ApplicationStart event. That's it.


  • How to add a new task with the Add method
  • The Add method is used to add a new task. Here are three ways in which 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 first two arguments of these overloads have the same behavior.

    The first argument "key" is a string. It specifies the name of the task. This name should be unique. This key may be used for future operations.

    The second argument "callback" is a WebTaskExecuteCallback. It specifies the callback method. This method will run if the task is executed. The method with the 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", is an integer. It specifies the 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", is a TaskExecutePeriod. It specifies the type of the task execution intervals. The value can be None, EveryNoon, Daily, Weekly, TwoWeekly, or Monthly. For example, if the value is set to Weekly, the task will run every weekends.

    The third argument of Method 3, "customPeriod", is a TimeSpan. Based on 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:

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

  • Getting the 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 the Add method. This method will return a WebTaskItem.

  • Synchronous access
  • There are two methods to lock and unlock the access to the 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 their definitions:

    //Method 1:
    //Method 2:
    Lock( wait )
    //Method 3:

    Call the Lock method to lock the access, and call the Unlock method to unlock the access. The second method has an argument that specifies the 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 a try..finally block when you're using these methods.

    Here is an example:

        WebTaskScheduler.Add("MyTask", MyTask_CallBack, 1);

    The VB.NET code:

     Dim onMytaskCallBack As WebTaskExecuteCallback
     onMytaskCallBack = New WebTaskExecuteCallback(AddressOf MyTask_CallBack)
     WebTaskScheduler.Add("MyTask", onMytaskCallBack, TaskExecutePeriod.Weekly)
    End Try
  • TaskExecutePeriod
  • NameDecription
    NoneExecutes the task immediately (not recommended)
    EveryNoonExecutes the task every noon
    DailyExecutes the task every night
    WeeklyExecutes the task every weekend at night
    TwoWeeklyExecutes the task every two weeks at night
    MonthlyExecutes the task every month at night
    ShamsiWeeklyExecutes the task every week at night using Persian dates
    ShamsiTwoWeeklyExecutes the task every two weeks at night using Persian dates
    ShamsiMonthlyExecutes 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 the 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 whose type is WebTaskItem. This can be modified in the callback.

Some tips

  1. Important: In your ASP.NET site, always add your tasks in the 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!


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


About the Author

Web Developer
Anonymous Proxy Anonymous Proxy
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDailybackup is not working Pin
Amitsp3-May-09 0:01
memberAmitsp3-May-09 0:01 
GeneralRe: Dailybackup is not working Pin
SalarSoft3-May-09 5:06
memberSalarSoft3-May-09 5:06 
Generalinteresting. Pin
Jason Witty3-Apr-08 14:12
memberJason Witty3-Apr-08 14:12 
GeneralRe: interesting. Pin
SalarSoft16-Apr-08 22:06
memberSalarSoft16-Apr-08 22:06 
QuestionWhat happens when no one is visiting? Pin
Thomas Eyde3-Apr-08 4:05
memberThomas Eyde3-Apr-08 4:05 
GeneralRe: What happens when no one is visiting? Pin
SalarSoft16-Apr-08 22:03
memberSalarSoft16-Apr-08 22:03 
GeneralRe: What happens when no one is visiting? Pin
Thomas Eyde17-Apr-08 0:38
memberThomas Eyde17-Apr-08 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180221.1 | Last Updated 24 Mar 2008
Article Copyright 2008 by SalarSoft
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid