private static async Task MainAsync() { var tenSeconds = TimeSpan.FromSeconds(60); var scheduler = new ActionScheduler(tenSeconds); scheduler.Schedule( TimeSpan.FromSeconds(1), () => WriteLine("First second")); scheduler.Schedule( TimeSpan.FromSeconds(5), () => WriteLine("Fifth second")); // this task run with delay but will be executed in correct time in the next iteration scheduler.Schedule( TimeSpan.FromSeconds(5), async() => { WriteLine("Fifth second with delay"); await Task.Delay(TimeSpan.FromSeconds(1)); }); await scheduler.StartAsync(); Console.ReadKey(); scheduler.Stop(); }
public async Task Scheduled_Tasks_Runs_Exactly_In_Time() { var task1 = GetFakeTimeInvoker(); var task2 = GetFakeTimeInvoker(); var sampleInSeconds = 5; var firstDelay = 2; var secondDelay = 3; var sampling = TimeSpan.FromSeconds(sampleInSeconds); DateTimeOffset startTime; var testScheduler = new TestScheduler(); using (var scheduler = new ActionScheduler(sampling, testScheduler)) { scheduler.Schedule(TimeSpan.FromSeconds(firstDelay), () => task1.Object.Invoke(testScheduler.Now)); scheduler.Schedule(TimeSpan.FromSeconds(secondDelay), () => task2.Object.Invoke(testScheduler.Now)); startTime = testScheduler.Now; await scheduler.StartAsync(); testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); } var correction = startTime.AddSeconds(-(startTime.Second % sampleInSeconds)); var firstTime = correction.AddSeconds(firstDelay); if (startTime > firstTime) { firstTime = firstTime.Add(sampling); } var secondTime = correction.AddSeconds(secondDelay); if (startTime > secondTime) { secondTime = secondTime.Add(sampling); } task1.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, firstTime))), Times.Exactly(1)); task1.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, firstTime.Add(sampling)))), Times.Exactly(1)); task2.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, secondTime))), Times.Exactly(1)); task2.Verify( x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, secondTime.Add(sampling)))), Times.Exactly(1)); }
private void RegisterTasks() { var actionScheduler = new ActionScheduler(Batidas, Configuracao); actionScheduler.Analize(); actionScheduler.Schedule(); }
public async Task Can_Schedule_Multiple_Tasks() { var task1 = GetFakeInvoker(); var task2 = GetFakeInvoker(); var testScheduler = new TestScheduler(); using (var scheduler = new ActionScheduler(TimeSpan.FromSeconds(5), testScheduler)) { scheduler.Schedule(TimeSpan.FromSeconds(1), () => task1.Object.Invoke()); scheduler.Schedule(TimeSpan.FromSeconds(2), () => task2.Object.Invoke()); await scheduler.StartAsync(); testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks); } task1.Verify(x => x.Invoke(), Times.Exactly(2)); task2.Verify(x => x.Invoke(), Times.Exactly(2)); }
private async Task ScheduleRepeatedMessages() { if (ActionScheduler != null) { var messages = await BotDataClient.GetTimedMessages(); var messageId = 0; foreach (var message in messages) { var action = new RepeatingMessage(message.Message, message.Delay, ConnectedClients, $"AutomatedMessage-{messageId}"); ActionScheduler.Schedule(action); messageId++; } } }
public async Task Can_Schedule_Task(TimeSpan startTime, bool inclusive, TimeSpan sampling, TimeSpan delay, TimeSpan workDuration, int occurs) { var invoker = GetFakeInvoker(); var testScheduler = new TestScheduler(); using (var scheduler = new ActionScheduler(sampling, testScheduler)) { scheduler.Schedule(delay, () => invoker.Object.Invoke(), inclusive); testScheduler.AdvanceBy(startTime.Ticks); await scheduler.StartAsync(); testScheduler.AdvanceBy(workDuration.Ticks - startTime.Ticks); } invoker.Verify(x => x.Invoke(), Times.Exactly(occurs)); }
private static bool HackingTool__AbortHack__Prefix(HackingTool __instance) { if (!__instance.m_isHacking) { return(HarmonyControlFlow.Execute); } // Execute the rest of the hacking sequence if interrupted // prematurely. switch (__instance.m_state) { case HackingTool.HackSequenceState.UpdateMiniGame: var timingGrid = __instance.m_activeMinigame.TryCast <HackingMinigame_TimingGrid>(); // Skip the TimingGrid's GamePauseTimer as it only updates // its state every so often. if (timingGrid?.m_puzzleDone != true) { return(HarmonyControlFlow.Execute); } __instance.Sound.Post(EVENTS.HACKING_PUZZLE_SUCCESS); CompleteHack(1); return(HarmonyControlFlow.DontExecute); case HackingTool.HackSequenceState.DoneWait: CompleteHack(1); return(HarmonyControlFlow.DontExecute); case HackingTool.HackSequenceState.Done: CompleteHack(); return(HarmonyControlFlow.DontExecute); default: return(HarmonyControlFlow.Execute); } void CompleteHack(float?delay = null) { var hackable = __instance.m_currentHackable; var sound = __instance.Sound; var player = __instance.Owner; __instance.ClearScreen(); __instance.m_activeMinigame?.EndGame(); __instance.OnStopHacking(); __instance.m_state = HackingTool.HackSequenceState.Idle; __instance.m_stateTimer = 0; __instance.m_currentHackable = null; if (delay == null) { Impl(); return; } ActionScheduler.Schedule(Impl, (float)delay); void Impl() { sound.Post(EVENTS.BUTTONGENERICSEQUENCEFINISHED); if (hackable == null) { return; } LG_LevelInteractionManager.WantToSetHackableStatus(hackable, eHackableStatus.Success, player); } } }