- Home /
Is there a way to tell if a particular coroutine is running?
Is there a way to tell if a particular coroutine is still running?
Imagine you have a class
public IEnumerator ImagineThis() {}
and it contains functions that are coroutines, perhaps one called IEnumertaor Foo().
Please, do not answer that Foo could set a flag while it is running, and you could look at that. The question is, can you look at ImagineThis in some way and determine if Foo() is "running" right now.
One would imagine that at some level, MonoBehaviour - or something - must keep track of which coroutines it has to come back to in the next frame; if so, there's the answer ???
Any ideas?
Noting this question ...
http://answers.unity3d.com/questions/541431/how-the-heck-do-you-check-if-start-is-still-runnin.html
Answering this question would additionally answer that question.
Answer by robhuhn · Sep 23, 2013 at 02:44 PM
I would say there is no way to determine which routines are running - Especially in a case where the coroutine is started by unity like
public IEnumerator Start()
{
yield return new WaitForSeconds(5f);
}
All routines end up being passed to external methods which should then be handled by c++ in a non-reachable scope.
[WrapperlessIcall ]
[MethodImpl (MethodImplOptions.InternalCall)]
public extern Coroutine StartCoroutine_Auto (IEnumerator routine);
[WrapperlessIcall ]
[MethodImpl (MethodImplOptions.InternalCall)]
public extern Coroutine StartCoroutine (string methodName, object value);
Please correct me if I'm wrong.
(You must be on the wrong site because you actually answered the question at hand :) )
I'm thinking ....... say, you can cancel a coroutine right? hell if invoke you can cancel it by name .. so maybe that suggests a way to know if it's still going?
I guess you've pretty much answered the question @Rob - it's "no, you can't" eh!
That seems to be the size of it - it is interesting to me since, I mean obviously "unity know" one is running, since it can easily be cancelled etc, and they get stopped if the gameObject is active-false'd, so, clearly, "unity must know" what coroutines are running.
I actually don't understand the complex ob-c you posted, or what it is! :) But it looks like you know what you're talking about
thanks again!
Answer by ArkaneX · Sep 23, 2013 at 11:02 PM
This is not exactly the info you're looking for, but please check this thread:
Answer by dropsofwaterfromchina · Aug 04, 2018 at 01:51 PM
public enum CoroutineState
{
Ready,
Running,
Finished
}
public class CoroutineController
{
private IEnumerator routine;
private Coroutine coroutine;
public CoroutineState state;
public CoroutineController(IEnumerator routine)
{
this.routine = routine;
state = CoroutineState.Ready;
}
public void Start()
{
if (state != CoroutineState.Ready)
{
throw new System.InvalidOperationException("Unable to start coroutine in state: " + state);
}
state = CoroutineState.Running;
coroutine = CoroutineHelper.Instance.StartCoroutine(RealRun());
}
private IEnumerator RealRun()
{
yield return CoroutineHelper.Instance.StartCoroutine(routine);
state = CoroutineState.Finished;
}
public void Stop()
{
if (state != CoroutineState.Running)
{
throw new System.InvalidOperationException("Unable to Stop coroutine in state: " + state);
}
CoroutineHelper.Instance.StopCoroutine(coroutine);
state = CoroutineState.Finished;
}
}
/// <summary>
/// 地球人自取。。。
/// </summary>
public class CoroutineHelper : MonoBehaviour
{
private static CoroutineHelper ins;
public static CoroutineHelper Instance
{
get
{
if (ins == null)
{
var go = new GameObject("CoroutineHelper");
DontDestroyOnLoad(go);
ins = go.AddComponent<CoroutineHelper>();
}
return ins;
}
}
public void StartCoroutineEx(IEnumerator routine, out CoroutineController coroutineController)
{
if (routine == null)
{
throw new System.ArgumentNullException("routine");
}
coroutineController = new CoroutineController(routine);
coroutineController.Start();
}
}
Answer by superventure · Oct 01, 2013 at 12:48 AM
You could always set up booleans that are only triggered when the coroutine is enabled, and ended when the coroutine is over.
hi super! Don't forget, that's precisely what the question's about, can you "actually look" and see, other than just setting a flag. ie, note I said "Please, do not answer that Foo could set a flag while it is running, and you could look at that." - heh!! Cheers...
Your answer
Follow this Question
Related Questions
Yield return null? 3 Answers
Wrapping Monobehavior? 0 Answers
WaitForEndOfFrame vs Time.frameCount 1 Answer
Serializable class with coroutines? 1 Answer