Last active
December 13, 2019 16:03
-
-
Save tintoy/8cfbe0eec43ec547f77161f477d326a6 to your computer and use it in GitHub Desktop.
Historical scheduling of events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Reactive; | |
| using System.Reactive.Concurrency; | |
| using System.Reactive.Linq; | |
| namespace HistoricalScheduling | |
| { | |
| static class Program | |
| { | |
| public static void Main() | |
| { | |
| try | |
| { | |
| DateTimeOffset now = DateTimeOffset.Now - TimeSpan.FromMinutes(5); | |
| HistoricalScheduler scheduler = new HistoricalScheduler(); | |
| scheduler.AdvanceTo(now); | |
| scheduler.Schedule(now + TimeSpan.FromSeconds(2), () => | |
| { | |
| Console.WriteLine("One"); | |
| }); | |
| scheduler.Schedule(now + TimeSpan.FromSeconds(3), () => | |
| { | |
| Console.WriteLine("Two"); | |
| }); | |
| scheduler.Schedule(now + TimeSpan.FromSeconds(5), () => | |
| { | |
| Console.WriteLine("Three"); | |
| }); | |
| Console.WriteLine("Ready."); | |
| Console.ReadLine(); | |
| Console.WriteLine("Running..."); | |
| const double timeDilationFactor = 1.0; | |
| TimeSpan quantum = TimeSpan.FromSeconds(0.5); | |
| IDisposable ticker = Observable.Interval(quantum).Subscribe(_ => | |
| { | |
| scheduler.AdvanceBy( | |
| quantum * timeDilationFactor | |
| ); | |
| }); | |
| using (ticker) | |
| { | |
| Console.ReadLine(); | |
| } | |
| Console.WriteLine("Done."); | |
| } | |
| catch (Exception unexpectedError) | |
| { | |
| Console.WriteLine(unexpectedError); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment