Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Scheduled Task in Middle Trust Enviroment

3.30/5 (4 votes)
11 Dec 2007CPOL2 min read 1  
Explains how to setup scheduled task on Windows hosting through Plesk CP

Introduction

I always wondered how to run scheduled tasks on windows hosting. For example, under the Plesk control. At first look, everything is ok. We have scheduled task and "command line" for it. But what to write there in the command line? This is the big question.

Somebody says - no problem, I'll write "iexplore http://example.com/track.aspx"

But I'm wondering, who closes this Internet Explorer process on server, and what if you are going to run this task every 5 minutes?

I don't like this way. So, where is the answer? Answer is inside VB. All that we need is to create VB script and run it inside command string.
This script creates Microsoft.XMLHTTP instance (yes, we don't need to check anything like in AJAX deal, because we are certainly in Microsoft environment). And using this instance requests your ASPX page to do the entire job.

So, when I've asked my hosting support, how to call aspx page scheduled - they said - this is impossible. Call PHP page and there request your ASPX if you want. No, thanks.

Using the Code

Plesk screen... to add scheduled task. "Path to executable" should be set to ABSOLUTE path to vbs file on server. Where can you get this path... First of all, your hoster should give you information about your site's folder. If not, write a simple aspx page with Server.MapPath("my.vbs"); So, what's inside the VB file?

VBScript
Call RunIt()
Sub RunIt()
        'Let the script to finish on an error.

        On Error Resume Next
        Dim RequestObj
        Dim URL
        Set RequestObj = CreateObject("Microsoft.XMLHTTP")
        'Request URL... 

        URL = "http://www.example.com/track.aspx"
        'Open request and pass the URL

        RequestObj.open "POST", URL , false
        'Send Request

        RequestObj.Send
        'cleanup

        Set RequestObj = Nothing
End Sub        

That's all. And you can run the scheduled task. Plesk is just for example... you can use Windows schedule if you've terminal access to the server.

Points of Interest

This is a simple thing, but I better say "trick" that can help you to solve the problem of scheduled task.

History

Originally posted on my site.

Works fine on several sites for daily content updates.

License

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