[Solved] How to call a function periodically every one hour in ASP.Net

Last Reply on Mar 20, 2015 12:39 AM By Mudassar

Posted on Mar 19, 2015 09:18 AM

I want to call a function on every after one hour. I wrote this function in Global.asax file on Aplication_Start event. Its work fine in Local Host but not working after publishing it on web server.

here is my code:

 

 protected void Application_Start(object sender, EventArgs e)
        {
            Application["UsersOnline"] = 0;
            
            System.Timers.Timer time = new System.Timers.Timer();
            time.Start();
            time.Interval = 600000; // 30 min //3.6e+6; // 1 hour
            time.Elapsed += time_elapsed;
        }

        public void time_elapsed(object sender, ElapsedEventArgs e)
        {
            DateTime value = new DateTime(DateTime.UtcNow.AddHours(5).Year, DateTime.UtcNow.AddHours(5).Month, DateTime.UtcNow.AddHours(5).Day,0,0,0);

            TimeSpan ts = DateTime.UtcNow.AddHours(5) - value;
            if (ts.Minutes >= 0 && ts.Minutes < 60 && ts.Hours == 0) // Creating range from :00 to :59
            {
                FeesClass.ApplyLateFees();
            }
        }

 

You are viewing reply posted by: Mudassar on Mar 20, 2015 12:39 AM.