Timed | Executes Using the Global.asa to schedule ASP code execution.
Have you ever had some ASP code this is needed to execute every once in a while but, and you just didn ' t know it.
There is a solution this doesn ' t involve running any scheduling or scripting software on the server and is actually very E Asy to get working.
There is thing called the "Global.asa". Most ASP Newbies probably wonder what the heck it even is. The Global.asa file is event driven. It can contain four event procedures:application_onstart, Application_OnEnd, Session_OnStart, and Session_OnEnd.
The Global.asa is basically loaded into memory the "a" a page on your WEB application. There are event procedure stubs that can contain script you want to is executed when the application starts or ends, or wh En the session starts or ends.
With some tricky coding the can use this file to schedule the code to execute. At least around the "time" you are need it to, this won ' t is able to make it execute at exactly a certain time.
This is the 1st example. It simply keeps track how many visitors have been to your site and over it resets the count back to 0 and executes Whatever code you need to run. Obviously you'll need to adjust the "M" to whatever makes sense for the amount of visitors site your.
Contents of the Global.asa are below.
<script Language=vbscript runat=server>
Sub Application_OnStart
Application ("sessioncount") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application ("Sessioncount") = Application ("Sessioncount") + 1
Application.UnLock
If application ("Sessioncount") > Then
Application.Lock
Application ("sessioncount") = 0
Application.UnLock
' Here's your would put any code for you need to run
' Don't surround the code with <%%> tags
' For example your might run a database query that checks for expired accounts
End If
End Sub
</SCRIPT>
Now lets say your want something to execute 4. can store the date & time in a text file and check it periodically. When the date is more than 6 hours the code would write the new date & time to the text file and th En execute the code for your want to run. You are could the "6" to whatever your want and therefore execute the code more or less often,
This is a pretty slick solution though it requires correct to the text file for permissions & reading. If not, you'll get a error.
In this example we are checking the text file every visitors. Can adjust this amount or remove the ' check ' completely so which it checks the file every time, but why check the file Every time do you have a very busy site? That's would just be a waste of the server resources, but it are up to you and far you want to take this.
In this example your need to start the text file is valid date& time or else you'll get a error because the Script would read in a empty value the 1st time.
Example:put 6/30/99 6:58:45 PM in the 1st line of the text file.
You could add code to check for that and handle the error, but I didn ' t really care in the time so I didn ' t doing that. As long as there is a date there when it starts it'll keep working.
Contents of the Global.asa are below.
<script Language=vbscript runat=server>
Sub Application_OnStart
Application ("sessioncount") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application ("Sessioncount") = Application ("Sessioncount") + 1
Application.UnLock
If application ("Sessioncount") > Then
Application.Lock
Application ("sessioncount") = 0
Application.UnLock
Set Objmyfile = CreateObject ("Scripting.FileSystemObject")
Set openmyfile = Objmyfile.opentextfile (Server.MapPath ("Last-update.txt"))
Myfilevalue = Openmyfile.readline
Openmyfile.close
If DateDiff ("H", Myfilevalue,now) > 6 Then
' Here's your would put any code for you need to run
' Don't surround the code with <%%> tags
' For example your might run a database query that checks for expired accounts
Set writemyfile = Objmyfile.createtextfile (Server.MapPath ("Last-update.txt"))
Writemyfile.writeline (now)
Writemyfile.close
End If
End If
End Sub