Dangerous .NET Timer Setting

StuartProgramming Blog

I had an idea to set my initial timer interval to 1ms so that when this service was initialized, the timer would fire immediately. It took me a while to find out that 1ms is a short enough time to frequently have multiple threads enter the cycleTimer_Elapsed section. Make sure your initial interval is a few seconds or you will see strange threading behavior.

	System.Timers.Timer cycleTimer = new Timer();
	cycleTimer.Elapsed += new ElapsedEventHandler(cycleTimer_Elapsed);
	//1 millisecond to start, first execution sets this to 60 seconds.  this will execute as soon as the timer runs
	cycleTimer.Interval = 1;
	cycleTimer.Start();
	sharedOperations.writeDebug("Starting Service on " + Environment.MachineName, debuglevel.status);

 

Tags: 

StuartDangerous .NET Timer Setting