Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- public class ElapseManager
- {
- public static readonly long TicksPerMillisecond = Stopwatch.Frequency / 1000;
- private readonly Dictionary<string, Elapser> ElapseCollection = new Dictionary<string, Elapser>();
- public bool ChangeInterval(string Name, long Ticks)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- ElapseCollection(Name).Interval = Ticks;
- return true;
- }
- return false;
- }
- public bool ChangeInterval(string Name, double Milliseconds)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- ElapseCollection(Name).Interval = Milliseconds * TicksPerMillisecond;
- return true;
- }
- return false;
- }
- public bool Check(string Name)
- {
- if (ElapseCollection.ContainsKey(Name))
- return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- return false;
- }
- public bool Check(string Name, long Ticks, bool TriggerOnFirstCheck = true)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- } else {
- if (Ticks > 0) {
- long nextTime = Stopwatch.GetTimestamp + Ticks;
- ElapseCollection.Add(Name, new Elapser(nextTime, Ticks));
- return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- } else {
- throw new Exception("Ticks must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
- }
- }
- }
- public bool Check(string Name, double Milliseconds, bool TriggerOnFirstCheck = true)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- } else {
- if (Milliseconds > 0) {
- long newTicks = Milliseconds * TicksPerMillisecond;
- long nextTime = Stopwatch.GetTimestamp + newTicks;
- ElapseCollection.Add(Name, new Elapser(nextTime, newTicks));
- return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- } else {
- throw new Exception("Milliseconds must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
- }
- }
- }
- public bool CheckRestart(string Name)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- bool Elapsed = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
- return Elapsed;
- }
- return false;
- }
- public bool CheckRestart(string Name, long Ticks, bool TriggerOnFirstCheck = true)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- bool Elapsed = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- if (Elapsed)
- ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
- return Elapsed;
- } else {
- if (Ticks > 0) {
- long nextTime = Stopwatch.GetTimestamp + Ticks;
- ElapseCollection.Add(Name, new Elapser(nextTime, Ticks));
- return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- } else {
- throw new Exception("Ticks must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
- }
- }
- }
- public bool CheckRestart(string Name, double Milliseconds, bool TriggerOnFirstCheck = true)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- bool Elapsed = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- if (Elapsed)
- ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
- return Elapsed;
- } else {
- if (Milliseconds > 0.0) {
- long newTicks = Milliseconds * TicksPerMillisecond;
- long nextTime = Stopwatch.GetTimestamp + newTicks;
- ElapseCollection.Add(Name, new Elapser(nextTime, newTicks));
- return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
- } else {
- throw new Exception("Milliseconds must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
- }
- }
- }
- public bool Remove(string Name)
- {
- return ElapseCollection.Remove(Name);
- }
- public bool Restart(string Name)
- {
- if (ElapseCollection.ContainsKey(Name)) {
- ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
- return true;
- }
- return false;
- }
- private class Elapser
- {
- public long NextTime;
- public long Interval;
- public Elapser(long NextTime, long Interval)
- {
- this.NextTime = NextTime;
- this.Interval = Interval;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement