Пример #1
0
        /// <summary>
        /// Executes the provided code block and while the code block is running, continually consume from
        /// the limit result provided one token each minute. This function allows the code to run for the
        /// first full minute without requesting additional time from the provider. Following that, every
        /// minute an additional one minute will be requested from the provider.
        /// </summary>
        /// <remarks>
        /// This method exists to support scheduled events, and as such, intercepts any errors raised via the
        /// provided code and wraps them in a <see cref="ScheduledEventException"/>. If in the future this is
        /// usable elsewhere, consider refactoring to handle the errors in a different fashion.
        /// </remarks>
        public static void Consume(
            this IIsolatorLimitResultProvider isolatorLimitProvider,
            ITimeProvider timeProvider,
            Action code,
            TimeMonitor timeMonitor
            )
        {
            var consumer = new TimeConsumer
            {
                IsolatorLimitProvider = isolatorLimitProvider,
                TimeProvider          = timeProvider
            };

            timeMonitor.Add(consumer);
            code();
            consumer.Finished = true;
        }