/// <summary> /// Creates a new instance of the <see cref="MachineTimer"/> class. /// </summary> /// <param name="info">Stores information about this timer.</param> /// <param name="owner">The machine that owns this timer.</param> public MachineTimer(TimerInfo info, Machine owner) { this.Info = info; this.Owner = owner; this.TimeoutEvent = new TimerElapsedEvent(this.Info); this.InternalTimer = new Timer(HandleTimeout, null, this.Info.DueTime, Timeout.InfiniteTimeSpan); }
/// <summary> /// Initializes a new instance of the <see cref="MachineTimer"/> class. /// </summary> /// <param name="info">Stores information about this timer.</param> /// <param name="owner">The machine that owns this timer.</param> public MachineTimer(TimerInfo info, Machine owner) { this.Info = info; this.Owner = owner; this.TimeoutEvent = new TimerElapsedEvent(this.Info); // To avoid a race condition between assigning the field of the timer // and HandleTimeout accessing the field before the assignment happens, // we first create a timer that cannot get triggered, then assign it to // the field, and finally we start the timer. this.InternalTimer = new Timer(this.HandleTimeout, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); this.InternalTimer.Change(this.Info.DueTime, Timeout.InfiniteTimeSpan); }