Added support for waiting for a task to finish with a timeout