Ejemplo n.º 1
0
 public Task EnqueueAsync(TItem item)
 {
     using (Lock.Enter())
     {
         ItemsQueue.Enqueue(item);
         return(LockedInsertUpdateAsync());
     }
 }
Ejemplo n.º 2
0
        //Do everything at once
        private void ProcessQueue()
        {
            string printStr = "----QUEUE DUMP-----\n";

            while (ItemsQueue.Count > 0)
            {
                printStr += $"{ItemsQueue.Dequeue().StrValue}\n";
            }

            printStr += "----------------";

            Console.WriteLine(printStr);
        }
Ejemplo n.º 3
0
        public QueueItem AddItem(string text, Func <QueueItem, CancellationToken, bool> actionWithResult, Action <QueueItem> actionWhenDone = null)
        {
            // Can't set default parameter to this in the function signature because it's not a compile-time constant
            QueueItem queueItem = new QueueItem()
            {
                TaskText = text, Status = QueueStatus.InQueue, Work = actionWithResult, DoWhenDone = actionWhenDone
            };

            ItemsQueue.Add(queueItem);

            Cycle();

            return(queueItem);
        }
Ejemplo n.º 4
0
        public bool TryDequeu(out TItem item)
        {
            using (Lock.Enter())
            {
                if (ItemsQueue.Count <= 0)
                {
                    item = default(TItem);
                    return(false);
                }

                item = ItemsQueue.Dequeue();
                LockedUpdateEnqueueAccess();
                return(true);
            }
        }
Ejemplo n.º 5
0
        public void AddItem(QueueItem item)
        {
//            Console.WriteLine($"Item added: {item.Type}");

            //Deal with first 2 items immediately
            if (ItemCount < ThrottleServiceConstant.ThrottleThreshold)
            {
                ProcessItem(item);
                ItemCount++;
                return;
            }

            ItemsQueue.Enqueue(item);
            ItemCount++;

            Stopwatch.Restart();
        }
Ejemplo n.º 6
0
        public void Cycle()
        {
            if (ItemsRunning < _appSettings.MaxBackgroundJobs)
            {
                var inQueueJobs = ItemsQueue.Where(x => x.Status == QueueStatus.InQueue);

                while (ItemsQueue.Where(x => x.Status == QueueStatus.InQueue).Count() > 0 && ItemsRunning < _appSettings.MaxBackgroundJobs)
                {
                    var nextJob = ItemsQueue.Where(x => x.Status == QueueStatus.InQueue).FirstOrDefault();

                    if (nextJob != null)
                    {
                        nextJob.Status = QueueStatus.InProgress;
                        QueueItemStatusChange.Invoke(nextJob);
                        StartItem(nextJob);
                    }
                }
            }
        }