Ejemplo n.º 1
0
 /// <summary>
 /// Invoke an async callback once after n milliseconds.
 /// </summary>
 /// <param name="milliseconds">The number of milliseconds to wait before the
 /// callback is invoked.</param>
 /// <param name="callback">The callback to invoke after n milliseconds.</param>
 public static async Task Once(double milliseconds, Action callback)
 {
     var timer = new PreciseTimer();
     await Task.Run(() =>
     {
         if (milliseconds < EPSILON)
         {
             callback();
             return;
         }
         Wait(milliseconds - timer.ElapsedMilliseconds);
         callback();
     });
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Invoke an async callback every time n milliseconds have passed.
 /// </summary>
 /// <param name="milliseconds">The number of milliseconds to wait before the
 /// callback is invoked.</param>
 /// <param name="callback">The callback to invoke every n milliseconds.
 /// When callback returns false the task is cancelled.</param>
 public static async Task Every(double milliseconds, Func <bool> callback)
 {
     var timer = new PreciseTimer();
     await Task.Run(() =>
     {
         if (milliseconds < EPSILON)
         {
             return;
         }
         while (true)
         {
             var w = timer.ElapsedMilliseconds % milliseconds;
             Wait(milliseconds - w);
             if (!callback())
             {
                 break;
             }
         }
     });
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Wait a specified number of milliseconds
 /// <remarks>A millisecond is a thousandth of a second</remarks>
 /// </summary>
 public static void Wait(double milliseconds)
 {
     PreciseTimer.Wait(milliseconds);
 }
Ejemplo n.º 4
0
 static Pi()
 {
     now = new PreciseTimer();
     PreciseTimer.Wait(1);
 }