Skip to content

Instantly share code, notes, and snippets.

@tintoy
Last active December 13, 2019 16:03
Show Gist options
  • Select an option

  • Save tintoy/8cfbe0eec43ec547f77161f477d326a6 to your computer and use it in GitHub Desktop.

Select an option

Save tintoy/8cfbe0eec43ec547f77161f477d326a6 to your computer and use it in GitHub Desktop.
Historical scheduling of events
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