# Poco

# template < class C >

# class Activity

Library: Foundation
Package: Threading
Header: Poco/Activity.h

## Description

This template class helps to implement active objects. An active object uses threads to decouple method execution from method invocation, or to perform tasks autonomously, without intervention of a caller.

An activity is a (typically longer running) method that executes within its own task. Activities can be started automatically (upon object construction) or manually at a later time. Activities can also be stopped at any time. However, to make stopping an activity work, the method implementing the activity has to check periodically whether it has been requested to stop, and if so, return. Activities are stopped before the object they belong to is destroyed. Methods implementing activities cannot have arguments or return values.

Activity objects are used as follows:

    class ActiveObject
    {
    public:
        ActiveObject():
            _activity(this, &ActiveObject::runActivity)
        {
            ...
        }

        ...

    protected:
        void runActivity()
        {
            while (!_activity.isStopped())
            {
                ...
            }
        }

    private:
        Activity<ActiveObject> _activity;
    };

## Inheritance

Direct Base Classes: Runnable

All Base Classes: Runnable

## Member Summary

Member Functions: isRunning, isStopped, run, start, stop, wait

Inherited Functions: run

## Types

### Callback

typedef typename RunnableAdapterType::Callback Callback;

### RunnableAdapterType

typedef RunnableAdapter < C > RunnableAdapterType;

## Constructors

### Activity

Activity(
C * pOwner,
Callback method
);

Creates the activity. Call start() to start it.

## Destructor

### ~Activity

~Activity();

Stops and destroys the activity.

## Member Functions

### isRunning

bool isRunning() const;

Returns true if the activity is running.

### isStopped

bool isStopped() const;

Returns true if the activity has been requested to stop.

### start

void start();

Starts the activity by acquiring a thread for it from the default thread pool.

### start

void start(
ThreadPool & pool
);

### stop

void stop();

Requests to stop the activity.

### wait

void wait();

Waits for the activity to complete.

### wait

void wait(
long milliseconds
);

Waits the given interval for the activity to complete. An TimeoutException is thrown if the activity does not complete within the given interval.

### run

void run();

See also: Poco::Runnable::run()