# Poco

# class TaskManager

Library: Foundation
Package: Tasks
Header: Poco/TaskManager.h

## Description

The TaskManager manages a collection of tasks and monitors their lifetime.

A TaskManager has a built-in NotificationCenter that is used to send out notifications on task progress and task states. See the TaskNotification class and its subclasses for the various events that result in a notification. To keep the number of notifications small, a TaskProgressNotification will only be sent out once in 100 milliseconds.

## Member Summary

Member Functions: addObserver, cancelAll, count, joinAll, postNotification, removeObserver, start, taskCancelled, taskFailed, taskFinished, taskList, taskProgress, taskStarted

## Types Aliases

### TaskList

using TaskList = std::list < TaskPtr >;

### TaskPtr

using TaskPtr = AutoPtr < Task >;

## Constructors

### TaskManager

TaskManager(
ThreadPool & pool
);

Creates the TaskManager, using the given ThreadPool (should be used by this TaskManager exclusively).

### TaskManager

TaskManager(
const std::string & name = "",
int minCapacity = 2,
int maxCapacity = 16,
int idleTime = 60,
int stackSize = 0
);

Creates the TaskManager.

## Destructor

### ~TaskManager

~TaskManager();

Destroys the TaskManager.

## Member Functions

### addObserver

void addObserver(
const AbstractObserver & observer
);

Registers an observer with the NotificationCenter. Usage:

    NObserver<MyClass, MyNotification> obs(*this, &MyClass::handleNotification);
    taskManager.addObserver(obs);

### cancelAll

void cancelAll();

Requests cancellation of all tasks.

### count

int count() const;

Returns the number of tasks in the internal task list.

### joinAll

void joinAll();

Waits for the completion of all the threads in the TaskManager's thread pool.

Note: joinAll() will wait for ALL tasks in the TaskManager's ThreadPool to complete. If the ThreadPool has threads created by other facilities, these threads must also complete before joinAll() can return.

### removeObserver

void removeObserver(
const AbstractObserver & observer
);

Unregisters an observer with the NotificationCenter.

### start

bool start(
Task * pTask
);

Starts the given task in a thread obtained from the thread pool; returns true if successful.

If this method returns false, the task was cancelled before it could be started, or it was already running; in any case, a false return means refusal of ownership and indicates that the task pointer may not be valid anymore (it will only be valid if it was duplicated prior to this call).

The TaskManager takes ownership of the Task object and deletes it when it is finished.

### taskList

TaskList taskList() const;

Returns a copy of the internal task list.

### postNotification

void postNotification(
const Notification::Ptr & pNf
);

Posts a notification to the task manager's notification center.

### taskCancelled

void taskCancelled(
Task * pTask
);

### taskFailed

void taskFailed(
Task * pTask,
const Exception & exc
);

### taskFinished

void taskFinished(
Task * pTask
);

### taskProgress

void taskProgress(
Task * pTask,
float progress
);

### taskStarted

void taskStarted(
Task * pTask
);

## Variables

### MIN_PROGRESS_NOTIFICATION_INTERVAL

static const int MIN_PROGRESS_NOTIFICATION_INTERVAL;