Async 1.8.0
Async::TaskRunner Class Reference

Run tasks from the Async main thread with automatic cleanup. More...

#include <AsyncTaskRunner.h>

Public Member Functions

 TaskRunner (void)
 Default constructor.
 
 TaskRunner (const TaskRunner &)=delete
 Copy constructor is deleted.
 
 ~TaskRunner (void)
 Destructor.
 
TaskRunneroperator= (const TaskRunner &)=delete
 Assignment operator is deleted.
 
template<typename TaskHandler >
Application::TaskId operator() (TaskHandler &&handler)
 Run a task from the Async main loop.
 
template<typename Func , typename Arg1 , typename... Args>
Application::TaskId operator() (Func &&f, Arg1 &&arg1, Args &&... args)
 Run a task from the Async main loop.
 
void cancel (void)
 Cancel all queued tasks for this object.
 

Detailed Description

Run tasks from the Async main thread with automatic cleanup.

Author
Tobias Blomberg / SM0SVX
Date
2019-03-09

This class is used to run delayed tasks from the Async main thread. If the object is destroyed before the task has been executed all tasks queued by this object are cancelled. That property is the main advantage for using this class rather than calling the Application::runTask functions directly.

A TaskRunner is typically used to break up long callback chains or to run a task on the main Async threa when running in another thread.

Definition at line 124 of file AsyncTaskRunner.h.

Constructor & Destructor Documentation

◆ TaskRunner() [1/2]

Async::TaskRunner::TaskRunner ( void )
inline

Default constructor.

Definition at line 130 of file AsyncTaskRunner.h.

◆ TaskRunner() [2/2]

Async::TaskRunner::TaskRunner ( const TaskRunner & )
delete

Copy constructor is deleted.

◆ ~TaskRunner()

Async::TaskRunner::~TaskRunner ( void )
inline

Destructor.

Will cancel all pending tasks

Definition at line 142 of file AsyncTaskRunner.h.

References cancel().

Member Function Documentation

◆ cancel()

void Async::TaskRunner::cancel ( void )
inline

Cancel all queued tasks for this object.

Definition at line 230 of file AsyncTaskRunner.h.

References Async::Application::app().

Referenced by ~TaskRunner(), and Async::ThreadSigCSignal< T_ret, Args >::~ThreadSigCSignal().

◆ operator()() [1/2]

template<typename Func , typename Arg1 , typename... Args>
Application::TaskId Async::TaskRunner::operator() ( Func && f,
Arg1 && arg1,
Args &&... args )
inline

Run a task from the Async main loop.

Parameters
fThe function to call
arg1The first argument to the function
argsThe rest of the arguments to the function
Returns
Return the task id associated with the task

This overloaded function is used if more than one argument is used in the function call. Calling a function taking two arguments can look like this:

runTask(&the_function, true, 42);

For a member function the second argument must be the object pointer so the syntax for a member function taking two arguments would look something like this:

runTask(&MyClass::func, this, true, 42);

This function is thread safe so a separate thread can add tasks which are then executed on the main Async thread.

Definition at line 221 of file AsyncTaskRunner.h.

References Async::Application::app(), and Async::Application::runTask().

◆ operator()() [2/2]

template<typename TaskHandler >
Application::TaskId Async::TaskRunner::operator() ( TaskHandler && handler)
inline

Run a task from the Async main loop.

Parameters
handlerThe handler function for the task to run
Returns
Return the task id associated with the task

Use it like this to call a normal function taking no arguments:

myTaskRunnerInstance(&the_function);

If the function takes arguments, you need to use std::bind but then it's better to use the other version of this function which calls bind for you. But anyway, with std::bind it will look something like this for a function taking a bool and an int as arguments:

myTaskRunnerInstance(std::bind(&the_function, true, 42));

For a member function the second argument to std::bind must be the object pointer so the syntax for a member function taking two arguments would look something like this:

myTaskRunnerInstance(std::bind(&MyClass::func, this, true, 42));

This function is thread safe so a separate thread can add tasks which are then executed on the main Async thread.

Definition at line 192 of file AsyncTaskRunner.h.

References Async::Application::app(), and Async::Application::runTask().

◆ operator=()

TaskRunner & Async::TaskRunner::operator= ( const TaskRunner & )
delete

Assignment operator is deleted.


The documentation for this class was generated from the following file: