The Task Manager framework provides an easy mechanism for constructing and executing complex sequences of operations. Each operation is encapsulated in a Task and can be associated with other operations in a variety of ways. This framework also provides a number of useful, reusable tasks for performing common operations. For example, here is how you could use the provided PlaySoundTask to play a sound:

var playSoundTask:PlaySoundTask =
	new PlaySoundTask( aLoadedSoundObject )
		.withCompleteHandler( completeHandler )
		.withErrorHandler( errorHandler )
		.run();
                    

In the above example, the completeHandler would be executed when the Sound completed playing successfully. If something went wrong during playback the errorHandler would be called. This is not very useful in and of itself however as the Sound class already provides a pretty easy interface for playing audio. But what if we wanted to create a chain of tasks that played a Sound once a piece of UI was initialized? This is also a bit of an arbitrary example but let's take a look at the syntax:

var waitForUIInitialization:WaitForUIInitialization = new WaitForUIInitialization( uiComponent );
var playSound:PlaySoundTask = new PlaySoundTask( loadedSound );

var waitForInitializationAndPlaySoundTask:CompositeTask =
	new CompositeTask(
		[ waitForUIInitialization, playSound ], false )
	.withCompleteHandler( completeHandler )
	.withErrorHandler( errorHandler )
	.run() as CompositeTask;
                    

In the above example, our sound is not played until the previous task has completed. If something goes wrong with the first task our errorHandler will be called and our Sound will not be played.

Using the Task Manager (or helper its tasks) it is easy to assemble a large sequence of operations that have dependencies on each other. One more example (although still a bit arbitrary) would be if we wanted to load an external file, wait for a UIComponent to initialize, and then play a sound once both of those operations had completed. Here's how we could set that up with Task Manager:

var loadFileTask:URLRequestTask = new URLRequestTask( urlRequest );
var waitForUIInitialization:WaitForUIInitialization = new WaitForUIInitialization( uiComponent );
var playSound:PlaySoundTask = new PlaySoundTask( loadedSound );

var taskManager:TaskManager = new TaskManager();
taskManager.addTask( loadFileTask );
taskManager.addTask( waitForUIInitialization );
taskManager.addTask( playSound, [ loadFileTask, waitForUIInitialization ] );
taskManager.run();
                    

In the above example, the TaskManager is configured to execute the URL loading task and the UIComponent-related task immediately. The task that plays our Sound however is marked as dependent upon the other 2 tasks and so the TaskManager will not execute it until they have both successfully completed. If either one fails then the TaskManager will dispatch an error event and our Sound-playing task will not be executed.

Although the convenience tasks provided with the TaskManager are useful, the real power of this framework can be seen when you create your own operations. This is typically done by extending Task or using a generic helper task like TaskWithClosure. Check out each of the classes below to see what kind of objects you have to build upon.



Interfaces
 InterfaceDescription
 IDecoratorTask Special interface for tasks that decorate other tasks.
 IInterruptibleTask An interruptible Task can be paused and resumed after being run.
 ISynchronousTask A synchronous Task is one that completes immediately upon being run.
 ITask A Task represents a job.
Classes
 ClassDescription
 AbstractCompositeTask This is an abstract class and should not be instantiated directly.
 AbstractTaskManager This is an abstract class and should not be instantiated directly.
 CompositeTask Wraps a set of ITasks and executes them in parallel or serial, as specified by a boolean constructor arg.
 EventHandlingTask Convenience Task that listens to an IEventDispatcher for either a success or failure type event.
 FactoryTask Decorates a Task returned by the specified factory method.
 InnocuousTaskDecorator Decorates a Task but re-dispatches both error and success events as success.
 InterruptibleCompositeTask Wraps a set of IInterruptibleTask (or ISynchronousTask) and executes them in parallel or serial, as specified by a boolean constructor arg.
 InterruptibleDecoratorTask Decorates a non-interruptible Task and adds interruptible-like behavior.
 InterruptibleStubTask Interruptible stub Task primarily intended for use in the context of automated tests.
 InterruptibleTask Abstract inerruptible Task.
 InterruptibleTaskManager Manages execution of a set of IInterruptibleTasks.
 ObserverTask Observes (but does not execute) a collection of Tasks.
 PlaySoundTask Task that plays a Sound and completes when the sound has finished playing.
 RetryOnFailureDecoratorTask Special Task decorator for Tasks that should be retried on failure.
 SleepTask Sleeps until told to complete.
 StubTask Empty Task useful primarily for unit testing.
 SynchronousTask Synchronous Task for convenience sub-class purposes.
 SynchronousTaskWithClosure 
 Task This class is meant to encapsulate a single, self-contained job.
 TaskManager Manages execution of a set of ITasks.
 TaskManagerComponentInfo Used for retrieving information about the TaskManager component.
 TaskWithClosure Task that invokes a specified function upon execution.
 TaskWithTimeout Decorates a Task and enforces a max-execution time limit.
 URLRequestTask Task that loads a URLRequest and exposes the URLLoader's "urlLoaderData" upon success.
 WaitForUIInitialization Convenience task for waiting until a specified UIComponent has initialized.
 WaitTask Task that delays for a specified time before completion.