예제 #1
0
 public void Enqueue(T msg)
 {
     m_Queue.Enqueue(msg);
     if (m_Queue.Count >= m_MaxBatchCount)
     {
         Interlocked.Exchange(ref m_Started, 1);
         m_FlushTimer.Change(0, Timeout.Infinite);
     }
     else if (Interlocked.Exchange(ref m_Started, 1) == 0)
     {
         m_FlushTimer.Change(m_ElapseMillisecond, Timeout.Infinite);
     }
 }
예제 #2
0
파일: TaskPool.cs 프로젝트: FlyLikeOpen/Fly
        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);
        }