private static void QueueOffloadTask() { try { QueueItemCallback currentItem = null; int queueItemsCount = 0; while (concurrentQueue.TryDequeue(out currentItem)) { try { currentItem?.Invoke(); queueItemsCount++; } catch (Exception e) { if (currentItem != null) { Global.logWriteException("Failed to invoke queued callback for {0}, skipping.", e, null, currentItem.Target.GetType().FullName); } else { Global.logWriteException("Failed to invoke queued callback for NULL, skipping.", e, null); } } } // Global.logWriteInformation("Finishing offload thread. Total {0} invocation made.", null, queueItemsCount); } finally { semaphore.Release(); } }
public static void Enqueue(QueueItemCallback item) { concurrentQueue.Enqueue(item); if (semaphore.CurrentCount > 0) { if (semaphore.Wait(60)) { Global.logWriteVerbose("Starting offload thread.", "QueueManager", "Enqueue", 0); Thread thread = new Thread(QueueOffloadTask); thread.Start(); } else { Global.logWriteVerbose("Another instance of offloading thread is already running.", "QueueManager", "Enqueue", 0); } } }