internal TimerPool_Item(TimerPool pool, TimerPoolItemCallback callback, object state, int timerInterval) { _pool = pool; _callback = callback; _state = state; _timerInterval = timerInterval; CurrentTimeRemaining = timerInterval; }
public TimerPool_Item Add(int timerInterval, TimerPoolItemCallback callback, object state = null, bool isStart = true) { var item = new TimerPool_Item(this, callback, state, timerInterval); lock (lockerObj) { var index = -1; //查找第一个小于10000个托管定时任务的定时列表 if (_delgateList.Count > 0) { for (int i = 0; i < _delgateList.Count; i++) { if (_delgateList[i].Count < 10000) { index = i; break; } } } //如果都大于或等于10000的话,则添加一个新的 if (index == -1) { var newList = new List <TimerPool_Item>(1000); _delgateList.Add(newList); index = _delgateList.Count - 1; var newtimer = new TimerEx(CheckerList, 2000, newList); _checkTimer.Add(newtimer); if (isStart) { newtimer.Start(); } } _delgateList[index].Add(item); if (_delgateList.Count > 3) { _delgateList.Sort((x, y) => x.Count.CompareTo(y.Count)); } } return(item); }
public void Close() { if (_pool == null || (_pool.IsDisposed || this._isDisposed)) { return; } _isStop = true; _pool.Remove(this); _callback = null; _pool = null; _timerInterval = 0; _isDisposed = true; }