/// <summary> /// 将某一刷新项添加到刷新列表中 /// </summary> /// <param name="refItem"></param> public void Enqueue(ConfigRefItem refItem) { lock (this) { // 检测是否已经添加过 foreach (var item in this._refList) { if (item.FilePath == refItem.FilePath) { return; } } this._refList.Add(refItem); } }
/// <summary> /// 开启刷新任务 /// </summary> public void Start() { if (Interlocked.Increment(ref IsStart) >= 2) { return; } new Task(() => { while (true) { this._errItems = new List <ConfigRefItem>(); // 从队列中取出需要刷新的配置文件 ConfigRefItem refItem = null; while ((refItem = ConfigRefQueue.Instance.Dequeue()) != null) { var obj = ConfigurationManager.LoadConfig(refItem.FilePath, refItem.ConfigEntityType); if (obj == null) { _errItems.Add(refItem); } else { // 刷新缓存 ConfigurationManager.Set(refItem.FilePath, obj); } } // 加载失败,重新添加到队列中 foreach (var item in this._errItems) { ConfigRefQueue.Instance.Enqueue(item); } Thread.Sleep(Interval * 1000); } }).Start(); }
/// <summary> /// 从刷新列表中取出一项 /// </summary> /// <returns></returns> public ConfigRefItem Dequeue() { ConfigRefItem entity = null; lock (this) { foreach (var item in this._refList) { if (item.AddTime.AddSeconds(Interval) <= DateTime.Now) { entity = item; } } if (entity != null) { this._refList.Remove(entity); } } return(entity); }