private void FlushQueue(object ar) { try { List <T> list = new List <T>(m_MaxBatchCount); do { T msg; if (m_Queue.Dequeue(out msg) < 0) // queue is empty. { DoAction(list); break; } list.Add(msg); if (list.Count >= m_MaxBatchCount) { DoAction(list); list.Clear(); } } while (true); } finally { m_FlushTimer.Change(m_ElapseMillisecond, Timeout.Infinite); } }
public TaskPool RegisterQueue <T>(Action <IEnumerable <T> > taskHandler, int pollingIntervalSeconds = 10, int queueCapacity = 10240, int maxBatchCount = 200, int threadCount = 1) where T : class { Type t = typeof(T); TaskOperator op; if (m_Operators.TryGetValue(t, out op) == false) { op = new TaskOperator { NextExecuteTime = DateTime.Now.AddSeconds(-1), PollingIntervalSeconds = pollingIntervalSeconds, ThreadCount = (threadCount <= 0 ? 1 : threadCount) }; IQueue <T> queue = new LocalMemoryQueue <T>(queueCapacity); op.Enqueue = task => queue.Enqueue((T)task); op.GetAllTasks = () => queue.GetAllItems(); op.Flush = () => { try { List <T> list = new List <T>(maxBatchCount); do { T msg; if (queue.Dequeue(out msg) < 0) // queue is empty. { if (list.Count > 0) { taskHandler(list); } break; } list.Add(msg); if (list.Count >= maxBatchCount) { taskHandler(list); list.Clear(); Thread.Sleep(10); } } while (true); } catch (Exception ex) { Logger.Error(ex); } }; m_Operators.Add(t, op); } return(this); }