public static IEnumerator Skip(int frames, IteratorTasks.CancellationToken ct) { for (int i = 0; i < frames; i++) { ct.ThrowIfCancellationRequested(); yield return(null); } }
public static Task Delay(int milliSecond, IteratorTasks.CancellationToken ct, TaskScheduler scheduler) { var tcs = new TaskCompletionSource <object>(scheduler); Timer timer = null; timer = new Timer(_ => { timer.Dispose(); tcs.SetResult(null); }, null, milliSecond, Timeout.Infinite); ct.Register(() => { timer.Dispose(); tcs.SetCanceled(); }); return(tcs.Task); }
/// <summary> /// n フレーム後に F1(x)を返すコルーチン。 /// キャンセル機能付き。 /// </summary> /// <param name="x">入力値。</param> /// <param name="n">コルーチン稼働のフレーム数。</param> /// <param name="completed">完了時に呼ばれるデリゲート。</param> /// <param name="ct">キャンセル用トークン。</param> /// <returns></returns> public static System.Collections.IEnumerator F1Cancelable(double x, int n, Action<double> completed, CancellationToken ct) { for (int i = 0; i < n; i++) { // キャンセルへの対処はあくまでコルーチン側の債務 // 例外を出して止める。 ct.ThrowIfCancellationRequested(); yield return null; } completed(F1(x)); }
public static Task Delay(int milliSecond, IteratorTasks.CancellationToken ct) { return(Delay(milliSecond, ct, Task.DefaultScheduler)); }
public static Task DelayFrame(int frames, IteratorTasks.CancellationToken ct, TaskScheduler scheduler) { return(Task.Run(Skip(frames, ct), scheduler)); }
/// <summary> /// 初期化。 /// </summary> public CancellationTokenSource() { Token = new CancellationToken(this); }
static System.Collections.IEnumerator Cancelで戻り値が切り替わるコルーチン(int n, Action<string> completed, CancellationToken ct) { var message = CompletedMessage; ct.Register(() => { message = CanceledMessage; }); for (int i = 0; i < n; i++) { if (ct.IsCancellationRequested) break; yield return null; } completed(message); }