# 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, operator =, run, start, stop, wait

Inherited Functions: run

## Types Aliases

### Callback

using Callback = typename RunnableAdapterType::Callback;

### RunnableAdapterType

using RunnableAdapterType = RunnableAdapter < C >;

## Constructors

### Activity

Activity() = delete;

### Activity

Activity(
const Activity & param154
) = delete;

### Activity

Activity(
C * pOwner,
Callback method
);

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

## Destructor

### ~Activity

~Activity() override;

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.

### operator =

Activity & operator = (
const Activity & param155
) = delete;

### 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() override;

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