Fiber.Delay.cs
Go to the documentation of this file.
1 using System;
2 using System.Threading;
3 using System.Collections;
4 
5 namespace SpicyPixel.Threading
6 {
7  public partial class Fiber
8  {
13  public static Fiber Delay (int millisecondsDelay)
14  {
15  return Delay (millisecondsDelay, CancellationToken.None);
16  }
17 
22  public static Fiber Delay (TimeSpan delay)
23  {
24  return Delay (CheckTimeout (delay), CancellationToken.None);
25  }
26 
32  public static Fiber Delay (TimeSpan delay, CancellationToken cancellationToken)
33  {
34  return Delay (CheckTimeout (delay), cancellationToken);
35  }
36 
42  public static Fiber Delay (int millisecondsDelay, CancellationToken cancellationToken)
43  {
44  return Delay(millisecondsDelay, cancellationToken, FiberScheduler.Current);
45  }
46 
53  public static Fiber Delay (int millisecondsDelay, CancellationToken cancellationToken, FiberScheduler scheduler)
54  {
55  if (millisecondsDelay < -1)
56  throw new ArgumentOutOfRangeException ("millisecondsDelay");
57 
58  return Fiber.Factory.StartNew(DelayCoroutine(millisecondsDelay, cancellationToken), scheduler);
59  }
60 
61  static IEnumerator DelayCoroutine(int millisecondsDelay, CancellationToken cancellationToken)
62  {
63  var startWait = DateTime.Now;
64  while (true) {
65  // Stop if cancellation requested
66  if (cancellationToken.IsCancellationRequested) {
67  yield break;
68  }
69 
70  // Stop if delay is passed
71  if (millisecondsDelay != Timeout.Infinite && (DateTime.Now - startWait).TotalMilliseconds >= millisecondsDelay) {
72  yield break;
73  }
74 
75  // FIXME: This would be preferable to above because it would let some
76  // schedulers sleep. It requires support for cancellation to be added, however.
77  //yield return new YieldForSeconds((float)millisecondsDelay / 1000f);
78 
79  yield return FiberInstruction.YieldToAnyFiber;
80  }
81  }
82  }
83 }
84 
Schedules fibers for execution.
Fiber StartNew(IEnumerator coroutine)
Start executing a new fiber using the default scheduler on the thread.
static FiberFactory Factory
Gets the default factory for creating fibers.
Definition: Fiber.cs:120
A Fiber is a lightweight means of scheduling work that enables multiple units of processing to execut...
Represents a fiber instruction to be processed by a FiberScheduler.
static Fiber Delay(int millisecondsDelay)
Crates a Fiber that waits for a delay before completing.
Definition: Fiber.Delay.cs:13
static YieldToAnyFiber YieldToAnyFiber
An instruction to cause the current fiber to yield to any ready fiber.
static FiberScheduler Current
Gets the default fiber scheduler for the thread.
static Fiber Delay(int millisecondsDelay, CancellationToken cancellationToken)
Crates a Fiber that waits for a delay before completing.
Definition: Fiber.Delay.cs:42
static Fiber Delay(TimeSpan delay)
Crates a Fiber that waits for a delay before completing.
Definition: Fiber.Delay.cs:22
static Fiber Delay(int millisecondsDelay, CancellationToken cancellationToken, FiberScheduler scheduler)
Crates a Fiber that waits for a delay before completing.
Definition: Fiber.Delay.cs:53
static Fiber Delay(TimeSpan delay, CancellationToken cancellationToken)
Crates a Fiber that waits for a delay before completing.
Definition: Fiber.Delay.cs:32