From d6ad4e1f2c262f0827e3fb4275f23b2c5aa03e9c Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 28 Feb 2020 22:27:38 +0100 Subject: [PATCH] allow querying finish states of coroutines --- .../{Coroutine.cs => ActiveCoroutine.cs} | 19 ++++++++++--------- Coroutine/CoroutineHandler.cs | 9 +++++---- Test/Example.cs | 13 ++++++++----- 3 files changed, 23 insertions(+), 18 deletions(-) rename Coroutine/{Coroutine.cs => ActiveCoroutine.cs} (58%) diff --git a/Coroutine/Coroutine.cs b/Coroutine/ActiveCoroutine.cs similarity index 58% rename from Coroutine/Coroutine.cs rename to Coroutine/ActiveCoroutine.cs index 369950b..066e0e7 100644 --- a/Coroutine/Coroutine.cs +++ b/Coroutine/ActiveCoroutine.cs @@ -1,34 +1,35 @@ using System.Collections.Generic; namespace Coroutine { - internal struct Coroutine { + public class ActiveCoroutine { private readonly IEnumerator enumerator; + public bool IsFinished { get; private set; } - public Coroutine(IEnumerator enumerator) { + internal ActiveCoroutine(IEnumerator enumerator) { this.enumerator = enumerator; this.enumerator.MoveNext(); } - public bool Tick(double deltaSeconds) { + internal bool Tick(double deltaSeconds) { var curr = this.enumerator.Current; if (curr != null && curr.Tick(deltaSeconds)) { if (!this.enumerator.MoveNext()) - return true; + this.IsFinished = true; } - return false; + return this.IsFinished; } - public bool OnEvent(Event evt) { + internal bool OnEvent(Event evt) { var curr = this.enumerator.Current; if (curr != null && curr.OnEvent(evt)) { if (!this.enumerator.MoveNext()) - return true; + this.IsFinished = true; } - return false; + return this.IsFinished; } - public WaitType GetCurrentType() { + internal WaitType GetCurrentType() { return this.enumerator.Current.GetWaitType(); } diff --git a/Coroutine/CoroutineHandler.cs b/Coroutine/CoroutineHandler.cs index bd07c20..7a05a1c 100644 --- a/Coroutine/CoroutineHandler.cs +++ b/Coroutine/CoroutineHandler.cs @@ -4,16 +4,17 @@ using System.Collections.Generic; namespace Coroutine { public static class CoroutineHandler { - private static readonly List TickingCoroutines = new List(); - private static readonly List EventCoroutines = new List(); + private static readonly List TickingCoroutines = new List(); + private static readonly List EventCoroutines = new List(); - public static void Start(IEnumerator coroutine) { - var inst = new Coroutine(coroutine); + public static ActiveCoroutine Start(IEnumerator coroutine) { + var inst = new ActiveCoroutine(coroutine); var type = inst.GetCurrentType(); if (type == WaitType.Tick) TickingCoroutines.Add(inst); else if (type == WaitType.Event) EventCoroutines.Add(inst); + return inst; } public static void InvokeLater(IWait wait, Action action) { diff --git a/Test/Example.cs b/Test/Example.cs index 81d60b0..451f925 100644 --- a/Test/Example.cs +++ b/Test/Example.cs @@ -9,8 +9,8 @@ namespace Test { private static readonly Event TestEvent = new Event(); public static void Main() { - CoroutineHandler.Start(WaitSeconds()); - CoroutineHandler.Start(PrintEvery5Seconds()); + var seconds = CoroutineHandler.Start(WaitSeconds()); + CoroutineHandler.Start(PrintEvery10Seconds(seconds)); CoroutineHandler.InvokeLater(new WaitSeconds(10), () => { Console.WriteLine("Raising test event"); @@ -39,14 +39,17 @@ namespace Test { Console.WriteLine("After 10 more seconds " + DateTime.Now); yield return new WaitSeconds(20); - Console.WriteLine("Done"); - Environment.Exit(0); + Console.WriteLine("First coroutine done"); } - private static IEnumerator PrintEvery5Seconds() { + private static IEnumerator PrintEvery10Seconds(ActiveCoroutine first) { while (true) { yield return new WaitSeconds(10); Console.WriteLine("The time is " + DateTime.Now); + if (first.IsFinished) { + Console.WriteLine("By the way, the first coroutine has finished!"); + Environment.Exit(0); + } } }