コード例 #1
0
        /// <summary>
        /// Initializes a new <see cref="IItemQueue{TItem}"/>.
        /// </summary>
        /// <param name="itemQueue">An <see cref="IItemQueue{TItem}"/>.</param>
        /// <param name="queueItemHandler">An <see cref="IQueueItemHandler{TItem}"/>.</param>
        /// <param name="queueProcessorSettings">An <see cref="ItemQueueProcessorSettings"/>.</param>
        /// <param name="logger">An <see cref="ILogger"/>.</param>
        /// <exception cref="ArgumentNullException">
        /// - <paramref name="itemQueue"/>
        /// - <paramref name="queueItemHandler"/>
        /// - <paramref name="queueProcessorSettings"/>
        /// - <paramref name="logger"/>
        /// </exception>
        public QueueProcessor(IItemQueue <TItem> itemQueue, IQueueItemHandler <TItem> queueItemHandler, IQueueProcessorSettings queueProcessorSettings, ILogger logger)
        {
            _ItemQueue              = itemQueue ?? throw new ArgumentNullException(nameof(itemQueue));
            _QueueItemHandler       = queueItemHandler ?? throw new ArgumentNullException(nameof(queueItemHandler));
            _QueueProcessorSettings = queueProcessorSettings ?? throw new ArgumentNullException(nameof(queueProcessorSettings));
            _Logger = logger ?? throw new ArgumentNullException(nameof(logger));

            var runningTaks = _RunningTasks = new TrackedList <Task>();

            _ProcessQueueLock = new SemaphoreSlim(1, 1);
            _TaskLock         = new SemaphoreSlim(1, 1);

            runningTaks.CountSetting.Changed += (newCount, oldCold) => ProcessQueue();
            itemQueue.QueueSize.Changed      += (newQueueSize, oldQueueSize) => ProcessQueue();
            itemQueue.HeldQueueSize.Changed  += (newQueueSize, oldQueueSize) => ProcessQueue();
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new <see cref="ItemQueueProcessor{TItem}"/>.
        /// </summary>
        /// <param name="itemQueue">The <see cref="IItemQueue{TItem}"/> to listen for.</param>
        /// <param name="itemQueueProcessorSettings">The <see cref="IItemQueueProcessorSettings"/>.</param>
        /// <param name="logger">An <see cref="ILogger"/>.</param>
        /// <param name="processItemFunc">An action to process the messages.</param>
        /// <exception cref="ArgumentNullException">
        /// - <paramref name="itemQueue"/>
        /// - <paramref name="itemQueueProcessorSettings"/>
        /// - <paramref name="logger"/>
        /// - <paramref name="processItemFunc"/>
        /// </exception>
        public ItemQueueProcessor(IItemQueue <TItem> itemQueue, IItemQueueProcessorSettings itemQueueProcessorSettings,
                                  ILogger logger, Func <TItem, CancellationToken, Task <bool> > processItemFunc)
        {
            var runningTaks = new TrackedList <Task>();

            _ItemQueue = itemQueue ?? throw new ArgumentNullException(nameof(itemQueue));
            _ItemQueueProcessorSettings = itemQueueProcessorSettings ?? throw new ArgumentNullException(nameof(itemQueueProcessorSettings));
            _Logger           = logger ?? throw new ArgumentNullException(nameof(logger));
            _ProcessItemFunc  = processItemFunc ?? throw new ArgumentNullException(nameof(processItemFunc));
            _StartLock        = new SemaphoreSlim(0, 1);
            _ProcessQueueLock = new SemaphoreSlim(1, 1);
            _RunningThreads   = runningTaks;

            _RunningTask = new Setting <Task>(defaultValue: null);

            runningTaks.CountSetting.Changed += async(newCount, oldCold) => await ProcessQueueAsync(CancellationToken.None).ConfigureAwait(false);

            itemQueue.QueueSize.Changed += async(newQueueSize, oldQueueSize) => await ProcessQueueAsync(CancellationToken.None).ConfigureAwait(false);

            itemQueue.HeldQueueSize.Changed += async(newQueueSize, oldQueueSize) => await ProcessQueueAsync(CancellationToken.None).ConfigureAwait(false);
        }