Esempio n. 1
0
        public AgeUpdatingService(IEnumerable <IBirthday> participants, IDispatcherTimer timer, int capacity = 4)
        {
            _timer        = timer;
            _timer.Tick  += OnTick;
            _participants = new OrderedList <KeyValuePair <TimeSpan, IBirthday> >(capacity, new KeyComparer <TimeSpan, IBirthday>());
            _participants.AddRange(participants.Select(p => new KeyValuePair <TimeSpan, IBirthday>(p.DateTimeBirth.TimeOfDay, p)));

            if (_participants.Any())
            {
                TimeSpan nowTime = DateTime.Now.TimeOfDay;
                _nextIndex = _participants.BinarySearch(new KeyValuePair <TimeSpan, IBirthday>(nowTime, null));
                if (_nextIndex < 0)
                {
                    _nextIndex = ~_nextIndex;
                }
                TimeSpan interval;
                if (_nextIndex >= _participants.Count)
                {
                    _nextIndex = 0;
                    interval   = TimeSpan.FromDays(1) - nowTime + _participants[0].Key;
                }
                else
                {
                    interval = _participants[_nextIndex].Key - nowTime;
                }
                StartTimer(interval);
            }
        }