/// <summary> /// Initializes a new instance of the SingletonRenewTimer class. /// </summary> /// <param name="keeper">The singleton keeper instance to maintain.</param> /// <param name="dueTime">The amount of time to delay before the callback parameter invokes its methods.</param> /// <param name="period">The time interval between invocations of the methods referenced by callback.</param> public SingletonRenewTimer(SingletonKeeper <T> keeper, TimeSpan dueTime, TimeSpan period) { Keeper = keeper ?? new SingletonKeeper <T>(default(T)); interval = period; timer = new Timer(state => { if (!Keeper.IsRenewing && Keeper.CanRenew) { RenewAsync(); } }, null, dueTime, period); }
public async Task TestSingletonKeeperAsync() { var i = 0; var singleton = new SingletonKeeper <int>(async() => { await Task.Delay(10); i++; return(i); }); Assert.AreEqual(0, singleton.Cache); var j = await singleton.RenewAsync(true); Assert.AreEqual(i, j); Assert.AreEqual(1, j); }
public override async Task ProcessAsync() { var singletonManager = new SingletonResolver(); singletonManager.Register <HttpClientVerb.NameAndDescription>(); var num = 12; singletonManager.Register(num); singletonManager.Register("one", new Lazy <int>(() => 1)); if (!singletonManager.TryResolve <HttpClientVerb.NameAndDescription>(out _)) { ConsoleLine.WriteLine("Failed to resolve."); } if (singletonManager.TryResolve("one", out num)) { ConsoleLine.WriteLine("Got! " + num); } if (singletonManager.TryResolve(out num)) { ConsoleLine.WriteLine("Got! " + num); } ConsoleLine.WriteLine("Got! " + singletonManager.Resolve(typeof(int), "one")); ConsoleLine.WriteLine(); var count = 0; var keeper = new SingletonKeeper <int>(async() => { await Task.Delay(50); return(count += 1); }); using (var refresher = new SingletonRenewTimer <int>(keeper, TimeSpan.FromMilliseconds(100))) { ConsoleLine.WriteLine("Started scheduler."); ConsoleLine.WriteLine(count); await Task.Delay(100); ConsoleLine.WriteLine(count); await Task.Delay(100); ConsoleLine.WriteLine(count); await Task.Delay(100); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Force renew 3 times."); Task.WaitAll(refresher.RenewAsync(), refresher.RenewAsync(), refresher.RenewAsync()); ConsoleLine.WriteLine(count); refresher.IsPaused = true; ConsoleLine.WriteLine("Paused."); await Task.Delay(100); ConsoleLine.WriteLine(count); await Task.Delay(200); ConsoleLine.WriteLine(count); refresher.IsPaused = false; ConsoleLine.WriteLine("Resumed."); await Task.Delay(200); ConsoleLine.WriteLine(count); } await Task.Delay(200); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Disposed scheduler."); await Task.Delay(200); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Renew again."); await keeper.RenewAsync(); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Disable renew by way 1."); keeper.FreezeRenew(TimeSpan.FromMilliseconds(100)); await keeper.RenewIfCanAsync(); ConsoleLine.WriteLine(count); await Task.Delay(200); await keeper.RenewIfCanAsync(); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Disable renew by way 2."); keeper.LockRenewSpan = TimeSpan.FromMilliseconds(100); await keeper.RenewIfCanAsync(); ConsoleLine.WriteLine(count); await Task.Delay(200); await keeper.RenewIfCanAsync(); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Disable renew by way 3."); keeper.IsRenewDisabled = true; await keeper.RenewIfCanAsync(); ConsoleLine.WriteLine(count); ConsoleLine.WriteLine("Done!"); }
/// <summary> /// Initializes a new instance of the SingletonRenewTimer class. /// </summary> /// <param name="keeper">The singleton keeper instance to maintain.</param> /// <param name="interval">The time interval between invocations of the methods referenced by callback.</param> /// <param name="immediately">true if renew immediately; otherwise, false, by default.</param> public SingletonRenewTimer(SingletonKeeper <T> keeper, TimeSpan interval, bool immediately = false) : this(keeper, immediately ? TimeSpan.Zero : interval, interval) { }
/// <summary> /// Initializes a new instance of the SingletonRenewTimer class. /// </summary> /// <param name="keeper">The singleton keeper instance to maintain.</param> public SingletonRenewTimer(SingletonKeeper <T> keeper) : this(keeper, TimeSpan.FromMilliseconds(Timeout.Infinite), TimeSpan.FromMilliseconds(Timeout.Infinite)) { }