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... | |
BackgroundThread * | then (BackgroundThread *other) |
Add another BackgroundThread's functions to the end of this one's. More... | |
template<typename Func > | |
BackgroundThread * | thenBackground (Func &&func) |
Add a function to run on a background thread. More... | |
template<typename Func > | |
BackgroundThread * | thenMainThread (Func &&func) |
Add a function to run on the main thread. More... | |
template<typename Func > | |
BackgroundThread * | thenBackgroundWithProgress (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... | |
BackgroundThread * | catchBackground (CatchFunction func) |
Add a function to run on a background thread in the event an exception is thrown. More... | |
BackgroundThread * | catchMainThread (CatchFunction func) |
Add a function to run on the main thread in the event an exception is thrown. More... | |
BackgroundThread * | finallyBackground (FinallyFunction func) |
Add a function to run on a background thread after all other functions, even if something threw. More... | |
BackgroundThread * | finallyMainThread (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 BackgroundThread * | create () |
Create a new background thread (but don't start it) More... | |
typedef std::function<void(std::exception_ptr exc)> BackgroundThread::CatchFunction |
typedef std::function<void(bool success) > BackgroundThread::FinallyFunction |
typedef std::function<bool(size_t, size_t)> BackgroundThread::ProgressFunction |
typedef std::function<QVariant(QVariant value)> BackgroundThread::ThenFunction |
|
inline |
Add a function to run on a background thread in the event an exception is thrown.
func | Function to run on background thread |
|
inline |
Add a function to run on the main thread in the event an exception is thrown.
func | Function to run on main thread |
|
inlinestatic |
Create a new background thread (but don't start it)
|
signal |
Called when all functions have been run.
result | Final result |
|
signal |
Called when an exception is thrown, after all catch functions have been run.
exception | Thrown exception |
|
inline |
Add a function to run on a background thread after all other functions, even if something threw.
func | Function to run on background thread |
|
inline |
Add a function to run on the main thread after all other functions, even if something threw.
func | Function to run on main thread |
|
inline |
Start the thread and run all its functions in sequence.
init | Argument for first function in the thread |
|
inline |
Add another BackgroundThread's functions to the end of this one's.
Will move functions out of other
other | BackgroundThread whose functions will be used |
|
inline |
Add a function to run on a background thread.
func | Function to run on background thread |
|
inline |
Add a function to run on a background thread, with a progress dialog that blocks the main thread while it runs.
parent | Parent widget for progress dialog |
title | Title of progress dialog |
text | Text of progress dialog |
cancel | Cancel button text for progress dialog |
func | Function to run on background thread |
|
inline |
Add a function to run on the main thread.
func | Function to run on main thread |
|
inline |
Block until the thread finishes.