Brass9.Threading.TimerInfo.SetInterval C# (CSharp) Method

SetInterval() public method

public SetInterval ( int milliseconds ) : void
milliseconds int
return void
		public void SetInterval(int milliseconds)
		{
			if (timer == null)
			{
				timer = new Timer(new TimerCallback(x =>
				{
					LastFired = DateTime.UtcNow;
					timerCallback();
				}));
			}

			Active = milliseconds != Timeout.Infinite;

			int dueTime = 0;
			if (!Active)
			{
				// If we're stopping the timer, dueTime should play along
				dueTime = Timeout.Infinite;
			}
			else
			{
				// If we're changing the timer interval from one int to another, dueTime should be the remainder of the new Interval
				if (Interval != Timeout.Infinite)
				{
					dueTime = milliseconds - (int)(DateTime.UtcNow - LastFired).TotalMilliseconds;
					if (dueTime < 0)
						dueTime = 0;
				}
				//else
				//{
					// Wasn't even running - leave dueTime at 0
				//}
			}
	
			Interval = milliseconds;


			timer.Change(dueTime, milliseconds);
		}

Usage Example

Example #1
0
 /// <summary>
 /// If the background task timer is running, changes its interval.
 /// If the timer isn't running, starts the timer (and so, the background task).
 /// </summary>
 /// <param name="interval">The timer interval in milliseconds.</param>
 public void SetInterval(int interval)
 {
     intervalTimer.SetInterval(interval);
 }