- 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
LVL 10
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.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">

    <asp:Timer ID="Timer1" runat="server">

// 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
        While MethodWorkThread.IsAlive
        End While
    End Sub

    Private Sub MethodToCall()
    End Sub

End Class

Open in new window

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:
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...
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.

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.
I don't know about the requirment.. but a simple 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..

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.
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....
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.
GlobaLevelProgrammerAuthor Commented:
Also this has to run 24/7/ that's a factor as well
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.

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 a little weak on the multi threading...but i will review your link above...
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...

>> is a collection threads all in 'queue' to be processed...
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

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.
GlobaLevelProgrammerAuthor Commented:
Apologies for the delay need to accept this comment:
35744266Author:Alfred1Date:05/12/11 02:01 AMExpert Comment
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.