Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-05-11
16
Medium Priority
?
1,913 Views
Last Modified: 2012-05-11
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....
0
Comment
Question by:GlobaLevel
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 2

Expert Comment

by:roshnipatel
ID: 35742797
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35743159
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
 
LVL 2

Expert Comment

by:roshnipatel
ID: 35743267
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 14

Expert Comment

by:dejaanbu
ID: 35744193
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
 
LVL 21

Accepted Solution

by:
Alfred A. earned 2000 total points
ID: 35744266
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
 
LVL 21

Expert Comment

by:Alfred A.
ID: 35744271
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35745990
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
 
LVL 21

Expert Comment

by:Alfred A.
ID: 35746549
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35746720
Also this has to run 24/7/365...so that's a factor as well
0
 
LVL 21

Expert Comment

by:Alfred A.
ID: 35746838
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35750275
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35750334
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35750569
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
 
LVL 72

Expert Comment

by:Qlemo
ID: 35992242
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
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35992243
Apologies for the delay need to accept this comment:
35744266Author:Alfred1Date:05/12/11 02:01 AMExpert Comment
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question