Skip to content

Instantly share code, notes, and snippets.

@sdether
Created January 12, 2012 07:29
Show Gist options
  • Select an option

  • Save sdether/1599256 to your computer and use it in GitHub Desktop.

Select an option

Save sdether/1599256 to your computer and use it in GitHub Desktop.
public class Coordinator<T> {
private readonly Queue<IEnumerator> _coroutines = new Queue<IEnumerator>();
public Coordinator(IEnumerable<Func<Coordinator<T>, IEnumerator>> coroutines) {
// add all coroutines to our queue of coroutines to execute
foreach(var coroutine in coroutines) {
_coroutines.Enqueue(coroutine(this));
}
}
public void Execute() {
// iterate over coroutines
while(_coroutines.Count > 0) {
// get next coroutine
var coroutine = _coroutines.Dequeue();
if(coroutine.MoveNext()) {
// if MoveNext was true, the coroutine expects to be resumed, so we put the coroutine back in the queue to
// call MoveNext again once we've given the other coroutines a chance
_coroutines.Enqueue(coroutine);
}
}
}
public T State { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment