InstantASP Support

Help & Support

Understanding Scheduled Task Files

Scheduled Tasks

A scheduled task is essentially a web page (.aspx, .asp, .php, .jsp) containing server side code. A task file can be written in any server side language you wish providing your web server can interpret and execute the language. Most .NET developers will typically create VB or C# ASP.NET (.aspx) based task files and upload these to the tasks directory, once a task file is uploaded and tested you can define how often the task should run and simply add a reference to the task file when creating your task.

Other programming languages are supported for task files, for example you could create a .php or .jsp based task file and reference this when creating or editing your task. If you choose to use an alternative programming language other than VB or C# for your task file you will need to add your own check to ensure requests are coming from the forum task manager.

It's important to remember that scheduled tasks run in a background thread, separate from the forum. Because of this if your scheduled task code throws an exception this will not be detected by the forum. You should manually test your task code ensuring you handle or resolve any exceptions that may occur before using your code within a task file.

An example of a VB ASP.NET (.aspx) task file is shown below, this example executes a number of TSQL statements from an external file to periodically reset the various forum data to it's default state, this could easily be enhanced to provide custom functionality.

<%@ Page Language="vb" %>

<script language="VB" runat="server">

' private currentcontext variable
Private CurrentContext As InstantASP.InstantForum.HttpContext.Current = InstantASP.InstantForum.HttpContext.Current.Current

Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

' IMPORTANT: You must ensure this check is included, this will ensure the request is coming from the task manager
If Not InstantASP.Common.Business.ScheduledTasks.IsRequestTaskManager(System.Web.HttpContext.Current.Request) Then

' display message indicating task files cannot be access directly
System.Web.HttpContext.Current.Response.Write("<h2>" & _
InstantASP.Common.Globalization.Resources.GetKey("TaskFileAccessDeniedMessage", CurrentContext.CurrentCulture) & "</h2>")

Else ' the request is from the task manager, everything is ok so perform our task

' -------------------------------------------------
' Task code starts here
' -------------------------------------------------

' this task resets the database to the default demonstration values
' todo this we read some sql from a text file on the server and
' execute this against the database

' this task could be re-used for any purpose as it simply executes
' sql code from a text file against the database. You can test the sql
' code from the text file within a tool such as query analyzer to
' ensure it completes successfully

' build full physical path to our file containing our SQL code to execute
Dim SQLPath As String = InstantASP.Common.Globals.Resolve.InstallPath & _
CurrentContext.CurrentForumSettings.TaskDirectory & "\ResetDatabase.config"

' quick check to ensure our sql code exists
If System.IO.File.Exists(SQLPath) Then

' now add the SQL code from the file into a local variable
Dim strSQLCode As String = InstantASP.Common.IO.File.GetTextFromFile(SQLPath)

' set the commandtimeout, this is useful if this is a long database operation
' typically 20-30 seconds is enough however this should be increase depending
' on the size of your data or the complexity of your query
Dim CommandTimeout As Int16 = 120

' now execute the code against the database
InstantASP.Common.Business.Maintenance.ExecuteSQL(strSQLCode, CommandTimeout)

End If

End If

End Sub


Once you've created, tested and uploaded your custom task file to the tasks directory you should create a task to enable the scheduled execution of the task file.

For more information of creating and editing scheduled tasks please refer to the Managing Scheduled Tasks section of this guide.

Optionally provide private feedback to help us improve this article...

Thank you for your feedback!

Comments require login or registration.


Product: InstantForum
Version: All Versions
Level: Novice
Article not rated yet.
Article has been viewed 5.5K times.
Last Modified: 4 Years Ago
Last Modified By: Ryan Healey


Similar Articles