/// <summary> /// 处理过期或已达上限的数据 /// </summary> private void Handler() { Stopwatch stopwatch = Stopwatch.StartNew(); while (true) { var count = _bag.Count; if (count >= _size || (count > 0 && stopwatch.ElapsedMilliseconds >= _timeout)) { var list = new List <T>(); for (int i = 0; i < count; i++) { if (_bag.TryTake(out T t)) { list.Add(t); } } OnBatched?.Invoke(this, list); stopwatch.Restart(); } else { ThreadHelper.Sleep(_timeout); } } }
/// <summary> /// 释放批量打包缓存 /// </summary> public void Dispose() { var count = _bag.Count; if (count > 0) { var list = new List <T>(); for (int i = 0; i < count; i++) { if (_bag.TryTake(out T t)) { list.Add(t); } } OnBatched?.Invoke(this, list); } }
private void Bacher_OnBatched(IBatcher sender, byte[] data) { var bacher = (Batcher)sender; OnBatched?.Invoke(bacher.Name, data); }
private void _classificationBatcher_OnBatched(string id, byte[] data) { OnBatched?.Invoke(id, data); }