A simple .NET coroutine framework

(测试Evernote同步到wordpress)

public static class Coroutine
{
    private static List< IEnumerator > coroutines = new List< IEnumerator >();

    private static bool Step( IEnumerator coroutine)
    {
        IEnumerator inner = coroutine.Current as IEnumerator;
        if (inner != null && Step(inner))
            return true ;
        return coroutine.MoveNext();
    }

    public static IEnumerator Wait( Func <bool > condition)
    {
        while (!condition()) yield return null ;
    }

    public static void Start( IEnumerator coroutine)
    {
        coroutines.Add(coroutine);
    }

    public static void Update()
    {
        for (int i = 0; i < coroutines.Count; ++i)
        {
            if (!Step(coroutines[i]))
                coroutines.RemoveAt(i--);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *