1

I have the need to create a queue-based system where multiple threads will add actions to be processed by the queue.

Sounds simple enough, but my actions will be asynchronous (with animations) so I do not want to start the next action in the queue until the animation of the previous action is completed.

How could I do this?

4

1 に答える 1

2

メソッドとイベントを使用してIActionインターフェースを作成できます。ExecuteCompleted

IAction次に、 sとpopのキューを作成し、現在のアイテムのハンドラーにExecute次のアイテムを作成できます。Completed

yield returns sのイテレータメソッドを作成し、それぞれが終了した後IActionにキューを呼び出すこともできます。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();
    ...
}
于 2010-11-17T01:44:50.960 に答える