コード例 #1
0
ファイル: QueueTimer.cs プロジェクト: FlyLikeOpen/Fly
        public QueueTimer(Action <List <T> > handler, int elapseMillisecond = 1000 * 60, bool autoStartFlush = false, int boundedCapacity = 10240, int maxBatchCount = 200)
        {
            m_Handler           = handler;
            m_Queue             = new LocalMemoryQueue <T>(boundedCapacity);
            m_ElapseMillisecond = elapseMillisecond;
            m_MaxBatchCount     = maxBatchCount;
            int dueTime = autoStartFlush ? m_ElapseMillisecond : Timeout.Infinite;

            m_Started    = autoStartFlush ? 1 : 0;
            m_FlushTimer = new Timer(new TimerCallback(FlushQueue), null, dueTime, 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);
        }