Advertisement
TizzyT

ElapseManager -TizzyT

Sep 29th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. public class ElapseManager
  7. {
  8.     public static readonly long TicksPerMillisecond = Stopwatch.Frequency / 1000;
  9.  
  10.     private readonly Dictionary<string, Elapser> ElapseCollection = new Dictionary<string, Elapser>();
  11.     public bool ChangeInterval(string Name, long Ticks)
  12.     {
  13.         if (ElapseCollection.ContainsKey(Name)) {
  14.             ElapseCollection(Name).Interval = Ticks;
  15.             return true;
  16.         }
  17.         return false;
  18.     }
  19.  
  20.     public bool ChangeInterval(string Name, double Milliseconds)
  21.     {
  22.         if (ElapseCollection.ContainsKey(Name)) {
  23.             ElapseCollection(Name).Interval = Milliseconds * TicksPerMillisecond;
  24.             return true;
  25.         }
  26.         return false;
  27.     }
  28.  
  29.     public bool Check(string Name)
  30.     {
  31.         if (ElapseCollection.ContainsKey(Name))
  32.             return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  33.         return false;
  34.     }
  35.  
  36.     public bool Check(string Name, long Ticks, bool TriggerOnFirstCheck = true)
  37.     {
  38.         if (ElapseCollection.ContainsKey(Name)) {
  39.             return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  40.         } else {
  41.             if (Ticks > 0) {
  42.                 long nextTime = Stopwatch.GetTimestamp + Ticks;
  43.                 ElapseCollection.Add(Name, new Elapser(nextTime, Ticks));
  44.                 return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  45.             } else {
  46.                 throw new Exception("Ticks must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
  47.             }
  48.         }
  49.     }
  50.  
  51.     public bool Check(string Name, double Milliseconds, bool TriggerOnFirstCheck = true)
  52.     {
  53.         if (ElapseCollection.ContainsKey(Name)) {
  54.             return ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  55.         } else {
  56.             if (Milliseconds > 0) {
  57.                 long newTicks = Milliseconds * TicksPerMillisecond;
  58.                 long nextTime = Stopwatch.GetTimestamp + newTicks;
  59.                 ElapseCollection.Add(Name, new Elapser(nextTime, newTicks));
  60.                 return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  61.             } else {
  62.                 throw new Exception("Milliseconds must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
  63.             }
  64.         }
  65.     }
  66.  
  67.     public bool CheckRestart(string Name)
  68.     {
  69.         if (ElapseCollection.ContainsKey(Name)) {
  70.             bool Elapsed = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  71.             ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
  72.             return Elapsed;
  73.         }
  74.         return false;
  75.     }
  76.  
  77.     public bool CheckRestart(string Name, long Ticks, bool TriggerOnFirstCheck = true)
  78.     {
  79.         if (ElapseCollection.ContainsKey(Name)) {
  80.             bool Elapsed = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  81.             if (Elapsed)
  82.                 ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
  83.             return Elapsed;
  84.         } else {
  85.             if (Ticks > 0) {
  86.                 long nextTime = Stopwatch.GetTimestamp + Ticks;
  87.                 ElapseCollection.Add(Name, new Elapser(nextTime, Ticks));
  88.                 return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  89.             } else {
  90.                 throw new Exception("Ticks must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
  91.             }
  92.         }
  93.     }
  94.  
  95.     public bool CheckRestart(string Name, double Milliseconds, bool TriggerOnFirstCheck = true)
  96.     {
  97.         if (ElapseCollection.ContainsKey(Name)) {
  98.             bool Elapsed = ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  99.             if (Elapsed)
  100.                 ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
  101.             return Elapsed;
  102.         } else {
  103.             if (Milliseconds > 0.0) {
  104.                 long newTicks = Milliseconds * TicksPerMillisecond;
  105.                 long nextTime = Stopwatch.GetTimestamp + newTicks;
  106.                 ElapseCollection.Add(Name, new Elapser(nextTime, newTicks));
  107.                 return TriggerOnFirstCheck ? true : ElapseCollection(Name).NextTime < Stopwatch.GetTimestamp;
  108.             } else {
  109.                 throw new Exception("Milliseconds must be greater than 0" + Constants.vbCrLf + Constants.vbTab + " - TizzyT");
  110.             }
  111.         }
  112.     }
  113.  
  114.     public bool Remove(string Name)
  115.     {
  116.         return ElapseCollection.Remove(Name);
  117.     }
  118.  
  119.     public bool Restart(string Name)
  120.     {
  121.         if (ElapseCollection.ContainsKey(Name)) {
  122.             ElapseCollection(Name).NextTime = Stopwatch.GetTimestamp + ElapseCollection(Name).Interval;
  123.             return true;
  124.         }
  125.         return false;
  126.     }
  127.  
  128.     private class Elapser
  129.     {
  130.         public long NextTime;
  131.         public long Interval;
  132.         public Elapser(long NextTime, long Interval)
  133.         {
  134.             this.NextTime = NextTime;
  135.             this.Interval = Interval;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement