11

I use the following code in a asp.net website.

On application init i call InitializeTimer() once.

The goal of the code was to run DoWork() once every hour (1 time per hour) .

I also wanted the code to execute on different time every loop so i added the random part.

The result i got was werid , i can not find a explaination why is happens.

The code executed the function after 2hrs , then again after 2hrs , then after 3hrs , then after 2hrs , and 2hrs again.****

Can anybody explain the reason?

using System.Timers;
....
private static random = new Random();
....
public static void InitializeTimer()
{
    tTimer = new Timer();
    tTimer.AutoReset = true;
    tTimer.Interval = TimeSpan.FromHours(1.0).TotalMilliseconds;
    tTimer.Elapsed += new ElapsedEventHandler(ClassName1.tMailer_Elapsed);
    tTimer.Start();
}

private static void tTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    tTimer.Interval += random.Next(-5, 5);

    DoWork();
}

Update:

  1. Please don't post "use windows service" , or "scheduled task". My question is for the following code I'm not looking for better alternatives. Also , during this test (10hrs) , website was with high traffic , iis pool did not restart!

  2. Based on the following MSDN: (http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx)

If the interval is set after the Timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

Is it possible that re-setting the interval in the elapsed function is the cause of the problem?

Meaning that when tTimer_Elapsed function is called the count is 1hr(min a few millisecond) and my code "tTimer.Interval += random.Next(-5, 5);" is adding another full hour to the Interval?

4
  • If you take out the random part, does the code work as expected? Commented Feb 13, 2010 at 1:35
  • i dont know , the code is running on producation server , i cant change it now (a wont be able for a few days) , thats why i asked the question to find a explaination. Commented Feb 13, 2010 at 10:39
  • So does that mean it performs as expected when testing locally? Commented Feb 13, 2010 at 16:45
  • RuSh, So in other words, you're asking us to help you do it the wrong way despite the fact that Microsoft advised against using System.Timers in ASP.NET? (see learn.microsoft.com/en-us/dotnet/api/…) Commented Dec 29, 2021 at 14:11

4 Answers 4

7

ASP.NET applications will get shut down when not in use. If someone hits your site, and then no more hits, it can get shut down. Your timer won't fire.

For this type of maintenance work you want to use a windows scheduled task or windows service.

Sign up to request clarification or add additional context in comments.

3 Comments

thx for the input. the website or iis pool were NOT restarted during the test. i belive something is wrong with my code.
@sharru IIS recycles your application without user intervention.
i aware of that , i log those events , no restart happened.
3

Check this out... Jeff Atwood actually discussed something similar. I guess it worked, but according to Jeff the site outgrew this method so they went to a dedicated task.

1 Comment

Yeah, probably the only reason that worked is because SO never idles out in IIS...
1

Since .net 4.5.2, there is a class called HostingEnvironment, it can do what you're asking, here is how to use: https://blog.mariusschulz.com/2014/05/07/scheduling-background-jobs-from-an-asp-net-application-in-net-4-5-2

The HostingEnvironment.QueueBackgroundWorkItem method lets you schedule small background work items. ASP.NET tracks these items and prevents IIS from abruptly terminating the worker process until all background work items have completed.

1 Comment

This doesn't help the OP's issue, which is that the timer is not getting fired at the correct interval due to IIS idling out the app pool.
0

I second Sams suggestion of using windows scheduled task to hit a page every hour. I tried and tried to get mine to work and it sort of worked. I went to a scheduled task and it has never failed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.