Sunday, February 27, 2011

Schedule a Windows Task to call an ASP.NET web page - ASP.NET - VB.NET, Windows Task Scheduler

The following is how you can schedule a windows task that can call an ASP.NET web page. Usually we want to schedule task from Windows. I have seen lots of questions regarding this in the past.

The cool thing about Windows task is that you can execute custom jobs to be executed in the server without the need of a user to be logged in.

The following code can be used to call ASPX web pages to run a specific job that needs to execute some sort of logic. The script is written in VB.NET, but the web page can be written with C# as the vbscript only is used to create the call from the server to the page.

The steps are as follow:

  • Write the Script (VB in this case)
  • Create an ASP.NET page 
  • Schedule the task through the VBS Script
1- Write the script

Call LogEntry()

Sub LogEntry()


        On Error Resume Next

        'variables
        Dim objRequest
        Dim URL

        Set objRequest = CreateObject("Microsoft.XMLHTTP")

        'URL link 
        URL = "http://www.YourDomain.com/MyWebPage.aspx

        'Open the HTTP request and pass the URL 
        objRequest.open "POST", URL , false

        'Send the HTML Request
        objRequest.Send

        'Set the object to nothing
        Set objRequest = Nothing

End Sub

2- Create an ASP.NET page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="tracklog.aspx.vb" Inherits="Tracklog" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title>Log Page</title>
</head>

<body>

<form id="form1" runat="server">

<div></div>

</form>

</body>

</html>

Code behind (Here we are presenting an example that returns a page object with the message MyWebPage.aspx was called)


Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class MyWebPageLog

    Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Response.write(“MyWebPage.aspx was called:” & System.DateTime.Now())

       End Sub

End Class
3- Scedule a windows task:

NOTE : All the information below has been copied and pasted here from here http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&sd=tech

With Scheduled Tasks, you can schedule any script, program, or document to run at a time that is most convenient for you. Scheduled Tasks starts every time that you start Windows XP and runs in the background, and it starts each task that you schedule at the time that you specify when you create the task.Using Scheduled Tasks
To open Scheduled Tasks, click Start, click All Programs, point to Accessories, point to System Tools, and then click Scheduled Tasks.

To schedule a new task:
  1. Double-click Add Scheduled Task to start the Scheduled Task Wizard, and then click Next in the first dialog box.
  2. The next dialog box displays a list of programs that are installed on your computer, either as part of the Windows XP operating system, or as a result of software installation.

    Use one of the following procedures:
    • If the program that you want to run is listed, click the program, and then click Next.
    • If you want to run a program, script, or document that is not listed, click Browse, click the folder and file that you want to schedule, and then click Open.
  3. Type a name for the task, and then choose one of the following options:
    • Daily
    • Weekly
    • Monthly
    • One time only
    • When my computer starts (before a user logs on)
    • When I log on (only after the current user logs on)
  4. Click Next, specify the information about the day and time to run the task, and then click Next.

    Note that the information about the day and time to run the task vary depending on the selection that you made in the previous wizard dialog box. For example, if you chose Weekly, you must indicate the day of the week, the time, and if the task should run every week, every 2 weeks, every 3 weeks, and so on.
  5. Type the name and password of the user who is associated with this task. Make sure that you choose a user with sufficient permissions to run the program. By default, the wizard selects the name of the user who is currently logged on.
  6. Click Next, and then click Finish after you verify the choices that you have made.

Using Advanced Options in Scheduled Tasks

If you want to change the configuration of the task, click Open in the advanced properties for the task before you click Finish. After you click Finish, the Properties dialog box opens for the task.

On the Schedule tab, you can change any of the scheduling options that you chose in the wizard, and you can also change the task configuration so that the task does not run too long, does not run if the computer is running on batteries (for laptops), and to specify whether or not the computer should be idle for the task to run.

NOTE: You can open the Properties dialog box for the task at any time if you open Scheduled Tasks, right-click the task, and then click Properties.

You cannot schedule a task so that it repeats in an interval less than one day; however, you can do this in the Properties dialog box:
  1. Click the Schedule tab, and then click Advanced.
  2. Click to select the Repeat task check box, and then specify the number of minutes or hours in which you want the task to be repeated.

I hope you can find this useful.

1 comment:

  1. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial, Ms sql server

    Thanks a lot..!
    ri80

    ReplyDelete

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!