Hi everybody.
Recently I ran into situation with System.Threading.Timer in my ASP.NET
application. I reinstalled Windows on one of my servers and got timers stop
firing events after while, they just stop functioning one-by-one.
Here is detailed explanation. I run application on 2 dedicated Win 2003
servers. Server A serves about 1000 active simultaneous users (150-200 aspx
page requests per second), Sever B serves about 400 users (80-100 aspx
requests per sec). Application is the same. Machine.config is identical.
maxWorkerThreads and maxIOThreads bumped to 40. minFreeThreads = 8. There
are about 25 static timers throuout the application and there are some
dynamicly created timers. Recently I realized that hosting company did not
install SP1 for some reason, but applied critical hotfixes. I asked to
reinstall OS on the servers, so far they did it on server B.
Now, after installing Win 2003 on server B, with SP1 and all recent
updates, and installing my application there, I'm getting my timers stop
working. Possible exceptions in timer events are silenced. Logging shows
that timers work normally for a few minutes and then next event just never
happens. If I force timer by calling it's Change method, it works for a few
minutes and then dies again.
Below is simplyfied code sample and a result log.
Again, it works fine on Win 2003 without servicepack, and does not on
Win 2003 SP1.
The timers are not accessed from any other place, they are being created
on app init and then managed in timed event method in timed event (see
code).
Eventlog does not show any threads being terminated.
Also, I noticed that even though .Net version is the same on servers A
and B (1.1.4322.573), the behavior is a bit different. In particular, on
server A bad viewstate causes exception with message "The viewstate is
invalid for this page and might be corrupted." while on server B exception
message includes as well client IP, requested URL, and viewstate.
Does anybody have any idea what all this about? Any hint? For now I had
to shut down server B and let server A handle all the work.
I've had similar problem with System.Timers.Timer before. Since I
switched to System.Threading.Timer, it was gone. And now... ups...
Code sample.
Global.asax.cs
namespace VR
{
public class Global : System.Web.HttpApplication
{
private Common.AppTimers appTimers;
protected void Application_Start(Object sender, EventArgs e)
{
appTimers = new Common.AppTimers();
}
}
}
vrTimers.cs
using System;
using System.Threading;
namespace VR.Common
{
public class AppTimers
{
private Timer tmTimedWork;
static private AppTimers appTimers;
public AppTimers()
{
try
{
VR.Logger.Log(VR.enumLogs.Logic, "Init timers");
tmTimedWork = new Timer(new TimerCallback(OnTimedWork),
null, 7000, 10000);
appTimers = this;
}
catch(Exception ee)
{
VR.Logger.Log(VR.enumLogs.Logic, "AppTimers(): " +
ee.Message);
throw;
}
}
public void OnTimedWork(object source)
{
VR.Logger.Log(VR.enumLogs.Trace, "OnTimedWork begin");
try
{
tmTimedWork.Change(120000, 600000);
lock(tmTimedWork)
{
long period = SomeClass.SomeStatic() * 1000;
tmTimedWork.Change(period, period);
VR.Logger.Log(VR.enumLogs.Trace, "OnTimedWork end
"+period.ToString());
}
}
catch (Exception ee)
{
tmTimedWork.Change(5000, 5000);
VR.Logger.Log(VR.enumLogs.Logic, "OnTimedWork:
"+ee.Message);
}
}
}
}
The log looks like this:
8/31/2005 4:37:46 AM: OnTimedWork begin
8/31/2005 4:37:46 AM: OnTimedWork end 1000
8/31/2005 4:37:47 AM: OnTimedWork begin
8/31/2005 4:37:47 AM: OnTimedWork end 1000
8/31/2005 4:37:48 AM: OnTimedWork begin
8/31/2005 4:37:48 AM: OnTimedWork end 1000
8/31/2005 4:37:49 AM: OnTimedWork begin
8/31/2005 4:37:49 AM: OnTimedWork end 1000
8/31/2005 4:37:50 AM: OnTimedWork begin
8/31/2005 4:37:50 AM: OnTimedWork end 1000
8/31/2005 4:37:51 AM: OnTimedWork begin
8/31/2005 4:37:51 AM: OnTimedWork end 1000
8/31/2005 4:37:52 AM: OnTimedWork begin
8/31/2005 4:37:52 AM: OnTimedWork end 1000
8/31/2005 4:37:53 AM: OnTimedWork begin
8/31/2005 4:37:53 AM: OnTimedWork end 1000
8/31/2005 4:37:54 AM: OnTimedWork begin
8/31/2005 4:37:54 AM: OnTimedWork end 1000
Dmitry.