Beispiel #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();
     });
 }
Beispiel #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;
             }
         }
     });
 }
Beispiel #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);
 }
Beispiel #4
0
 static Pi()
 {
     now = new PreciseTimer();
     PreciseTimer.Wait(1);
 }