1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System;
- using System.Collections.Generic;
- namespace XGame.Framework.Time
- {
- /// <summary>
- /// 定时器仓储
- /// </summary>
- public class TimerRepository : IClockTick, IDisposable
- {
- private readonly List<Timer> _aliveTimers = new List<Timer>();
-
- internal ITimer AddDelayTimer(int delay, Action action)
- {
- var timer = new Timer(delay, delay, 1, action);
- _aliveTimers.Add(timer);
- return timer;
- }
- internal ITimer AddLooperTimer(int delay, int interval, Action<int> action, int loopTime = -1)
- {
- var timer = new Timer(delay, interval, loopTime, action);
- _aliveTimers.Add(timer);
- return timer;
- }
- public void ClockUpdate(int millisecond)
- {
- // 客户端定时器数量较少不考虑时间轮优化
- for (int i = _aliveTimers.Count - 1; i >= 0; --i)
- {
- if (_aliveTimers[i].Update(millisecond))
- {
- _aliveTimers.RemoveAt(i);
- }
- }
- }
- public void Dispose()
- {
- foreach (var item in _aliveTimers)
- {
- item.Dispose();
- }
- _aliveTimers.Clear();
- }
- }
- }
|