BackgroundThread Class Reference

Detailed Description

Helper class for running chains of actions on both the main thread and a background thread.
Especially useful for doing ui that also needs networking.
Think of it like a JS-like promise chain except with more C++.

Example:

    BackgroundThread::create()

Do actions serially in the background ->thenBackground([this](QVariant) { bool success = SomeLongNetworkOperation(); Return value will be passed to next action's QVariant parameter return success; }) And serially on the main thread ->thenMainThread([this](QVariant var) { Retrieve value from last action bool success = var.value<bool>(); UpdateUI(success); You don't have to return anything (next QVariant param will be QVariant()) }) You can also combine with a ProgressTask for showing a progress dialog ->thenBackgroundWithProgress(m_window, "Doing Task", "Please wait...", "Cancel", [this](QVariant var, ProgressTask* task, ProgressFunction progress) { progress(0, 0); DoTask1WithProgress(SplitProgress(progress, 0, 1)); You can interface with the task itself task->setText("Doing Part 2"); DoTask2WithProgress(SplitProgress(progress, 1, 1)); progress(1, 1); }) You can combine with another BackgroundThread to do its actions after all of the ones you have enqueued so far ->then(SomeOtherFunctionThatReturnsABackgroundThread()) If any then-action throws, all future then-actions will be ignored and the catch-actions will be run, serially NB: If a catch-action throws, the new exception will be passed to any further catch-actions ->catchMainThread([this](std::exception_ptr exc) { So far the only way I've found to get the exception out: try { std::rethrow_exception(exc); } catch (std::exception e) { Handle exception } }) You can also catch in the background ->catchBackground([this](std::exception_ptr exc) { ... }) Finally-actions will be run after all then-actions are finished If a then-action throws, finally-actions will be run after all catch-actions are finished NB: Finally-actions should not throw exceptions ->finallyMainThread([this](bool success) { if (success) { ReportSuccess(); } }) You can also have finally-actions in the background ->finallyBackground([this](bool success) { ... }) Call start to start the thread ->start();

Public Types

typedef std::function< bool(size_t, size_t)> ProgressFunction
 
typedef std::function< QVariant(QVariant value)> ThenFunction
 
typedef std::function< void(std::exception_ptr exc)> CatchFunction
 
typedef std::function< void(bool success) > FinallyFunction
 

Signals

void done (QVariant result)
 Called when all functions have been run. More...
 
void fail (std::exception_ptr exception)
 Called when an exception is thrown, after all catch functions have been run. More...
 

Public Member Functions

void start (QVariant init=QVariant())
 Start the thread and run all its functions in sequence. More...
 
void wait ()
 Block until the thread finishes. More...
 
BackgroundThreadthen (BackgroundThread *other)
 Add another BackgroundThread's functions to the end of this one's. More...
 
template<typename Func >
BackgroundThreadthenBackground (Func &&func)
 Add a function to run on a background thread. More...
 
template<typename Func >
BackgroundThreadthenMainThread (Func &&func)
 Add a function to run on the main thread. More...
 
template<typename Func >
BackgroundThreadthenBackgroundWithProgress (QWidget *parent, const QString &title, const QString &text, const QString &cancel, Func &&func)
 Add a function to run on a background thread, with a progress dialog that blocks the main thread while it runs. More...
 
BackgroundThreadcatchBackground (CatchFunction func)
 Add a function to run on a background thread in the event an exception is thrown. More...
 
BackgroundThreadcatchMainThread (CatchFunction func)
 Add a function to run on the main thread in the event an exception is thrown. More...
 
BackgroundThreadfinallyBackground (FinallyFunction func)
 Add a function to run on a background thread after all other functions, even if something threw. More...
 
BackgroundThreadfinallyMainThread (FinallyFunction func)
 Add a function to run on the main thread after all other functions, even if something threw. More...
 

Static Public Member Functions

static BackgroundThreadcreate ()
 Create a new background thread (but don't start it) More...
 

Member Typedef Documentation

◆ CatchFunction

typedef std::function<void(std::exception_ptr exc)> BackgroundThread::CatchFunction

◆ FinallyFunction

typedef std::function<void(bool success) > BackgroundThread::FinallyFunction

◆ ProgressFunction

typedef std::function<bool(size_t, size_t)> BackgroundThread::ProgressFunction

◆ ThenFunction

typedef std::function<QVariant(QVariant value)> BackgroundThread::ThenFunction

Member Function Documentation

◆ catchBackground()

BackgroundThread * BackgroundThread::catchBackground ( CatchFunction  func)
inline

Add a function to run on a background thread in the event an exception is thrown.

Parameters
funcFunction to run on background thread
Returns
This BackgroundThread

◆ catchMainThread()

BackgroundThread * BackgroundThread::catchMainThread ( CatchFunction  func)
inline

Add a function to run on the main thread in the event an exception is thrown.

Parameters
funcFunction to run on main thread
Returns
This BackgroundThread

◆ create()

static BackgroundThread * BackgroundThread::create ( )
inlinestatic

Create a new background thread (but don't start it)

Returns
Empty thread with no functions

◆ done

void BackgroundThread::done ( QVariant  result)
signal

Called when all functions have been run.

Parameters
resultFinal result

◆ fail

void BackgroundThread::fail ( std::exception_ptr  exception)
signal

Called when an exception is thrown, after all catch functions have been run.

Parameters
exceptionThrown exception

◆ finallyBackground()

BackgroundThread * BackgroundThread::finallyBackground ( FinallyFunction  func)
inline

Add a function to run on a background thread after all other functions, even if something threw.

Parameters
funcFunction to run on background thread
Returns
This BackgroundThread

◆ finallyMainThread()

BackgroundThread * BackgroundThread::finallyMainThread ( FinallyFunction  func)
inline

Add a function to run on the main thread after all other functions, even if something threw.

Parameters
funcFunction to run on main thread
Returns
This BackgroundThread

◆ start()

void BackgroundThread::start ( QVariant  init = QVariant())
inline

Start the thread and run all its functions in sequence.

Parameters
initArgument for first function in the thread

◆ then()

BackgroundThread * BackgroundThread::then ( BackgroundThread other)
inline

Add another BackgroundThread's functions to the end of this one's.

Will move functions out of other

Parameters
otherBackgroundThread whose functions will be used
Returns
This BackgroundThread

◆ thenBackground()

template<typename Func >
BackgroundThread * BackgroundThread::thenBackground ( Func &&  func)
inline

Add a function to run on a background thread.

Parameters
funcFunction to run on background thread
Returns
This BackgroundThread

◆ thenBackgroundWithProgress()

template<typename Func >
BackgroundThread * BackgroundThread::thenBackgroundWithProgress ( QWidget *  parent,
const QString &  title,
const QString &  text,
const QString &  cancel,
Func &&  func 
)
inline

Add a function to run on a background thread, with a progress dialog that blocks the main thread while it runs.

Parameters
parentParent widget for progress dialog
titleTitle of progress dialog
textText of progress dialog
cancelCancel button text for progress dialog
funcFunction to run on background thread
Returns
This BackgroundThread

◆ thenMainThread()

template<typename Func >
BackgroundThread * BackgroundThread::thenMainThread ( Func &&  func)
inline

Add a function to run on the main thread.

Parameters
funcFunction to run on main thread
Returns
This BackgroundThread

◆ wait()

void BackgroundThread::wait ( )
inline

Block until the thread finishes.


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