public async Task ExecuteActionOnDelay(int delayInMinutes) { try { if (!ActionIsActive) { ActionIsActive = true; DelayInMinutes = delayInMinutes; ReportingHelpers.LogInfo($"Broadcasting Initial Change Emitter message. Delay in minutes: {DelayInMinutes}."); DelayChangeEmitters.ForEach(emitter => emitter(DelayInMinutes)); while (DelayInMinutes > 0) { var cyclesPerMinute = 1000; for (int index = 0; index < cyclesPerMinute; index++) { await Task.Delay(MinuteInMs / cyclesPerMinute); if (CancelAction) { CancelAction = false; ActionIsActive = false; ReportingHelpers.LogInfo($"Broadcasting Cancelled Emitter message."); DelayCancelEmitters.ForEach(emitter => emitter()); return; } } DelayInMinutes--; ReportingHelpers.LogInfo($"Broadcasting Change Emitter message. Remaining delay in minutes: {DelayInMinutes}."); DelayChangeEmitters.ForEach(emitter => emitter(DelayInMinutes)); } ReportingHelpers.LogInfo($"Executing delayed action."); DefinedAction(); ActionIsActive = false; } } catch (Exception e) { ReportingHelpers.LogError($"Error executing Delayed Action: {e}"); } }
public void RegisterDelayCancelEmitter(Action delayCancelEmitter) { DelayCancelEmitters.Add(delayCancelEmitter); }