public Coroutine(Action action, IYieldBase condition, IPlugin owner, string name = null, bool infinity = true, bool autoStart = true) : this(name, owner) { Running = autoStart; Started = DateTime.Now; Owner = owner; TimeoutForAction = condition switch { WaitTime time => time.Milliseconds.ToString(), WaitRender render => render.HowManyRenderCountWait.ToString(), WaitRandom random => random.Timeout, WaitFunction _ => "Function -1", _ => TimeoutForAction }; Action = action; Condition = condition; if (infinity) { IEnumerator CoroutineAction(Action a) { yield return(YieldBase.RealWork); while (true) { try { a?.Invoke(); } catch (Exception e) { Console.WriteLine($"Coroutine {Name} in {OwnerName} error -> {e}"); } Ticks++; yield return(Condition.GetEnumerator()); } } _enumerator = CoroutineAction(action); } else { IEnumerator CoroutineAction(Action a) { yield return(Condition.GetEnumerator()); a?.Invoke(); Ticks++; } _enumerator = CoroutineAction(action); } }
public Coroutine(Action action, IYieldBase condition, IPlugin owner, string name = null, bool infinity = true, bool autoStart = true) : this(name, owner) { Running = autoStart; Started = DateTime.Now; switch (condition) { case WaitTime _: TimeoutForAction = ((WaitTime)condition).Milliseconds.ToString(); break; case WaitRender _: TimeoutForAction = ((WaitRender)condition).HowManyRenderCountWait.ToString(); break; case WaitRandom _: TimeoutForAction = ((WaitRandom)condition).Timeout; break; case WaitFunction _: TimeoutForAction = "Function -1"; break; } Action = action; Condition = condition; if (infinity) { IEnumerator CoroutineAction(Action a) { yield return(YieldBase.RealWork); while (true) { try { a?.Invoke(); } catch (Exception e) { Console.WriteLine($"Coroutine {Name} in {OwnerName} error -> {e}"); } Ticks++; yield return(Condition.GetEnumerator()); } } _enumerator = CoroutineAction(action); } else { IEnumerator CoroutineAction(Action a) { yield return(Condition.GetEnumerator()); a?.Invoke(); Ticks++; } _enumerator = CoroutineAction(action); } }