Asp.net - need to run a method every 5 minutes

I have a web site and I need to run a method every 5 minutes...any way to do this without an infinite loop or sleep method...I know of windows services but I'm a little new to that...I'm using vb.net....
LVL 10
GlobaLevelProgrammerAsked:
Who is Participating?
 
Alfred A.Commented:
You could try running your method in a background thread using AJAX.  You need to drag an Ajax Extension ScriptManager and Manager to this.  I created a quick sample below.  Try it out.  The sample below runs Response.Write("Hello") inside a method every 15 seconds.


// ASPX

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm4.aspx.vb" Inherits="TestApp1.WebForm4" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:Timer ID="Timer1" runat="server">
    </asp:Timer>
    </form>   
</body>
</html>


// Code Behind

Imports System.Threading

Public Class WebForm4
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Timer1.Interval = 15000  ''For 5 minutes set this to 300000 
    End Sub

    Dim MethodWorkThread As Thread

    Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        MethodWorkThread = New Thread(AddressOf MethodToCall)
        MethodWorkThread.IsBackground = True
        MethodWorkThread.Start()
        While MethodWorkThread.IsAlive
            Thread.Sleep(1)
        End While
    End Sub

    Private Sub MethodToCall()
        Response.Write("Hello")
        MethodWorkThread.Abort()
    End Sub

End Class

Open in new window

0
 
roshnipatelCommented:
You may want to write a simple console app that calls the function once in your main().  Then Schedule the exe to run every 5 min from the Window's Scheduler.

Setting it up as a service is also a good way to handle it.  
Here's an article that helps with that:
http://support.microsoft.com/kb/317421
0
 
GlobaLevelProgrammerAuthor Commented:
great idea...however if its a console app...I need to pass values from my website to the .exe and the results from the .exe back to the website...I should have mentioned that b4...
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
roshnipatelCommented:
This isn't the best suggestion I've given, but without knowing why you need this, maybe you should write the values from your site to a text file (or db) and then use the console app to read the values and run the function.

If you can share a little more about what you are doing and why, I'm sure we can come up with a better soln.
0
 
dejaanbuCommented:
I don't know about the requirment.. but a simple thought..in timer also, you can achieve this.

 <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="300000"></asp:Timer>

but not sure whether this is a good idea for u..

0
 
Alfred A.Commented:
Oops.  Correction on my previous post.  What I meant with "You need to drag an Ajax Extension ScriptManager and Manager to this." was

You need to drag an Ajax Extension ScriptManager and Timer.
0
 
GlobaLevelProgrammerAuthor Commented:
Some great suggestions...the goal is to ping a bunch of ip addresses and domain names at different time intervals and return the results back to the site for reporting needs....
0
 
Alfred A.Commented:
Well, the use of a background thread would in a way address your situation.  However, be aware that there are limits to the number of threads you can use depending on memory and so on.  Also, you might want to have a look at ThreadPool for multithreading.

http://msdn.microsoft.com/en-us/library/0ka9477y.aspx
0
 
GlobaLevelProgrammerAuthor Commented:
Also this has to run 24/7/365...so that's a factor as well
0
 
Alfred A.Commented:
OK.  Why don't you just run a standalone multithreading Windows Application then if 24/7/365.  At least you have more control to it than Windows Service.  Also, a web site is really not suitable if this is the case.  

You can still use multiple background threads with multiple timers if you want in an application.  Or, you could create instances of the Windows application where you could for instance select your target URL in a dropdownlist and each application runs a background thread independent on the other background thread.   There are pros and cons to this.  It will be bit harder to maintain if everything is done in one application but it will be cleaner.  With different instances, logic would be cleaner in each app but it would be hard managing all of those applications if you have say 15 apps running at the same time.   However, you are assured that each thread is really independent of each other and the chance of crashing the app would be less than stacking them all in one instance.

0
 
GlobaLevelProgrammerAuthor Commented:
OK.  Why don't you just run a standalone multithreading Windows Application then if 24/7/365.  At least you have more control to it than Windows Service.  Also, a web site is really not suitable if this is the case.  

>> fine with me..im a little weak on the multi threading...but i will review your link above...
0
 
GlobaLevelProgrammerAuthor Commented:
I reviewed the article you posted..a few questions if I may:

thread to block for long periods of time
>>Does this mean that the background worker thread is idle for some time('Block')

single-threaded apartment.
>> is a collection of threads whether foreground or background that have similar properties...

Threadpool
>> is a collection threads all in 'queue' to be processed...
0
 
GlobaLevelProgrammerAuthor Commented:
also in the threadpool examples....
Executing Asynchronous Tasks with the Task Parallel Library
Executing Code Asynchronously with QueueUserWorkItem
Supplying Task Data for QueueUserWorkItem
Using RegisterWaitForSingleObject

...which would I use if I have 5 processes:

process1: run every 5 minutes
process2: run every 10 minutes
process3: run every 30 minutes
process4: run every 60 minutes
process5: once a day




0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
GlobaLevelProgrammerAuthor Commented:
Apologies for the delay need to accept this comment:
35744266Author:Alfred1Date:05/12/11 02:01 AMExpert Comment
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.