public async Task <bool> StartupAsync(BatchHandler <TRequest, TResponse> batchHandler, TimeSpan minimumTimeInterval, TimeSpan maximumTimeInterval, int maximumCount)
        {
            await ShutdownAsync();

            if (minimumTimeInterval > maximumTimeInterval)
            {
                throw new ArgumentException("Maximum time interval cannot be less than the minimum time interval");
            }

            if (minimumTimeInterval <= TimeSpan.Zero)
            {
                throw new ArgumentException("Minimum time interval must be greater than zero");
            }

            this.userBatchProcessorHandler = batchHandler;
            this.MinimumTimeInterval       = minimumTimeInterval;
            this.MaximumTimeInterval       = maximumTimeInterval;
            this.MaximumCount = maximumCount;
            IsRunning         = true;

            batchProcessor = new AsyncListProcessor <List <InternalBatchProcessorItem> >(batchProcessor_DoWork, () => IsRunning);
            if (!await batchProcessor.StartupAsync())
            {
                TraceQueue.Trace(this, TracingLevel.Warning, "Failed to startup batch processor.  Shutting down...");
                await ShutdownAsync();

                return(false);
            }

            return(true);
        }
        public async Task StartupShutdownTest()
        {
            var processor = new AsyncListProcessor <AsyncListTestObject>(processItem);

            Assert.IsTrue(await processor.StartupAsync(), "Failed to startup list processor");
            Assert.IsTrue(processor.IsRunning, "IsRunning was false after startup");

            //Process some items
            var items = new List <AsyncListTestObject>();

            for (int i = 0; i < 10; i++)
            {
                var item = new AsyncListTestObject();
                items.Add(item);
                processor.Add(item);
            }
            Assert.IsTrue(Task.WaitAll(items.Select(item => item.Task).ToArray(), 10000), "Failed to process task lists");

            await processor.ShutdownAsync();

            Assert.IsFalse(processor.IsRunning, "IsRunning still true after shutdown");
        }
        /// <summary>
        /// Shutdown the current batch processor
        /// </summary>
        /// <param name="maxWait">Maximum amount of time, in milliseconds, to wait for any pending operation to finish</param>
        /// <returns></returns>
        public async Task ShutdownAsync(int maxWait = -1)
        {
            IsRunning = false;

            using (var releaser = await queueLock.LockAsync())
            {
                if (batchTimer != null)
                {
                    batchTimer.Change(-1, -1);
                    batchTimer.Dispose();
                    batchTimer = null;
                }
            }

            if (batchProcessor != null)
            {
                await batchProcessor.ShutdownAsync(maxWait);

                batchProcessor = null;
            }

            queue = null;
        }