0

I use Asp .NET Mvc 3 for creating web page and I need to change something in database after each 20 minutes... I set Timer in my Global.asax.cs file . Here is the code

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    Unit = new UnitOfWork();
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1200000; //20 minutes
    timer.Elapsed += new System.Timers.ElapsedEventHandler(Elapsed);
    timer.Start();
}

void Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    Unit.Srvc.UpdateUserActivity();
}

Now I run it today, and what a pitty, it works only one time... After 20 minutes it change database and it's all.

P.S.Yesteday I tested it in 20 seconds and it works fine. But,today it don't want to work correctly in 20 minutes interval. Thank you for help.

P.S.2 I used Stored Procedure for updating database.

P.S.3 Just now I detect that it works randomly :D In 5:32Am I run the program... It works in 5:52Am, doesn't work in 6:12Am, and works now(now is 6:49 Am, I don't know when it works).

Milan Solanki
  • 1,207
  • 4
  • 25
  • 48
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123

4 Answers4

4

Most likely cause is that your AppDomain is shutting down due to inactivity, which means the entire application is not running. The idle timeout is 20 minutes of inactivity, I think.

See this question:

How to keep ASP.NET assemblies in AppDomain alive?

Community
  • 1
  • 1
Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
  • I just have tested it in localhost... And application is active ;-) – Chuck Norris Aug 05 '11 at 05:57
  • 2
    +1 - Agreed this is likely the problem. But I would think through whether you really want to go to this effort and subvert IIS' normal behaviour which is there to help with the performance and reliability of your application(s) and server. – Reddog Aug 05 '11 at 06:06
1

To me it looks like your timer will be killed by garbage collection as you are not keeping a reference to it after it goes out of scope from Application_Start. Try adding:

Application["Whatever"] = timer;
alun
  • 3,393
  • 21
  • 26
0

You might be finding that your thread (from the IIS AppPool) is being recycled or shut down.

Web applications typically work best when used for request-response processing rather than this type of behaviour. It's not clear what you are up to, but assuming you are using SQL Server perhaps you could look at maintenance tasks or triggers if it involves denormalizing data (i.e. rolling up calculated data). If it involves data collected during the request-response process then perhaps you might look at using the web cache and some cache expiration operations for the delayed persistence.

Reddog
  • 15,219
  • 3
  • 51
  • 63
0

My guess is too that the session simply expires but I would like to add a little extra.

Reading the code I guess (again) that you are marking the user in the database as 'not active' or disconnected or something like that. If so, do not use a timer to do this, instead, set the session expiration (when the user hasn't sent any requests for a certain period) to the required duration and put the code you want to run when that happens in the Session_OnEnd handler

Emond
  • 50,210
  • 11
  • 84
  • 115