AccidentalFish.ApplicationSupport.Core.Policies.Implementation.AsynchronousIntervalTimer.ExecuteAsync C# (CSharp) Метод

ExecuteAsync() публичный Метод

public ExecuteAsync ( Func function, System.Action shutdownAction = null ) : Task
function Func
shutdownAction System.Action
Результат Task
        public async Task ExecuteAsync(Func<Task<bool>> function, Action shutdownAction = null)
        {
            if (_delayOnExecute)
            {
                await _taskDelay.Delay(_interval);
            }
            bool shouldContinue = true;

            while (shouldContinue)
            {
                shouldContinue = await function();
                if (shouldContinue)
                {
                    await _taskDelay.Delay(_interval);
                }                
            }

            shutdownAction?.Invoke();
        }
    }

Same methods

AsynchronousIntervalTimer::ExecuteAsync ( Func function, CancellationToken cancellationToken, System.Action shutdownAction = null ) : Task

Usage Example

        public async Task DelayOnExecuteWaitsForIntervalBeforeFirstTaskWithCancellationToken()
        {
            // Arrange
            Mock<IAsynchronousDelay> delay = new Mock<IAsynchronousDelay>();
            AsynchronousIntervalTimer timer = new AsynchronousIntervalTimer(delay.Object, TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), true);
            CancellationTokenSource source = new CancellationTokenSource();

            // Act
            await timer.ExecuteAsync(ct => Task.FromResult(false), source.Token);

            // Assert
            delay.Verify(x => x.Delay(TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), source.Token));
        }
All Usage Examples Of AccidentalFish.ApplicationSupport.Core.Policies.Implementation.AsynchronousIntervalTimer::ExecuteAsync