Вы можете создать интерфейс IAction
с методом Execute
и событием Completed
.
Затем вы можете создать очередь из IAction
s и pop и Execute
следующий элемент в обработчике Completed
для текущего элемента.
Вы можете даже создать метод итератора, который yield return
s IAction
s и будет иметь вызов очереди MoveNext()
после каждого IAction
завершения.
РЕДАКТИРОВАТЬ : Например:
class ActionEnumerator : IAction {
readonly IEnumerator<IAction> enumerator;
public ActionEnumerator(IEnumerator<IAction> enumerator) {
this.enumerator = enumerator;
}
public void Execute() {
//If the enumerator gives us another action,
//hook up its Completed event to continue the
//the chain, then execute it.
if (enumerator.MoveNext()) {
enumerator.Current.Completed += delegate { Execute(); };
enumerator.Current.Execute();
} else //If the enumerator didn't give us another action, we're finished.
OnCompleted();
}
}
IEnumerator<IAction> SomeMethod() {
...
yield return new SomeAction();
//This will only run after SomeAction finishes
...
yield return new OtherAction();
...
}