Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Timers;

C#
public partial class copy_option : System.Web.UI.Page
{
    private System.Timers.Timer aTimer;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(120000);
        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += OnTimer;
        aTimer.Enabled = true;
    }

    public void OnTimer(Object source, ElapsedEventArgs e)
    {
        importdatafromexcel(excelfilepath);
    }
}



Guys,

I wanted to run "importdatafromexcel" a function, after every 2 minutes but if I run the above code, the function was called twice for every minute. Please let me know what changes needs to be done.
Posted

1 solution

The problem is your understanding of web applications...
Such an application runs separated, means that there is a server and a client part of it...
What actually happening is that your page starts and creates the timer (on the server), than gets into the final phase of the page rendering, after which it dies!(unloads) and with it the timer too...
This is the point that you have to think about your solution, and to think about why at the first place you try to do such a thing!!!
I'm saying you this because such repeating action does not fit good into a web application...It may be better to create some sort of service...
After you have thinking about it...
There is an other timer class inside System.Web.UI namespace that came to solve your problem...It has a server ad a client side that wotk together to keep the timer alive even after the page on the server gone (using JavaScript)...
https://msdn.microsoft.com/en-us/library/system.web.ui.timer%28v=vs.110%29.aspx[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900