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 …

Answering this question would additionally answer that question.

This is not exactly the info you’re looking for, but please check this thread:

http://forum.unity3d.com/threads/202064-Extended-coroutines

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.

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();
    }
}

You could always set up booleans that are only triggered when the coroutine is enabled, and ended when the coroutine is over.