public static void Do() { var @continue = File.Exists(@"./timer.txt"); if (@continue) { Continue(); return; } var storageEngine = new SimpleFileStorageEngine(@"./timer.txt", false); var scheduler = new PoormansExecutionEngine(); scheduler.Start(storageEngine, false); scheduler.Schedule(() => Console.WriteLine("Hello From Anonymous Function")); var cw = new ConsoleWriter("Hello From Console Writer"); new PersistentTimer( DateTime.Now + TimeSpan.FromSeconds(10), cw.Do, false, scheduler ); }
public PersistentTimer(DateTime expiration, Action action, bool hasBeenExecuted, PoormansExecutionEngine executionEngine) { _expiration = expiration; _action = action; _hasBeenExecuted = hasBeenExecuted; _executionEngine = executionEngine; if (!_hasBeenExecuted) { executionEngine.Schedule(Start); } }
private void Start() { _executionEngine.Entangle(this); var now = DateTime.Now; var delay = _expiration - now; if (delay.Ticks < 0) { delay = TimeSpan.Zero; } Task.Delay(delay).ContinueWith(_ => { _executionEngine.Schedule(() => { _action(); _hasBeenExecuted = true; _executionEngine.Untangle(this); }); }); }