2017-12-28 07:23:20 +01:00
|
|
|
|
#pragma once
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
#include <functional>
|
2017-10-05 16:00:16 +02:00
|
|
|
|
#include <QRunnable>
|
2017-12-28 07:23:20 +01:00
|
|
|
|
//#include <QVector>
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
|
#include <QWaitCondition>
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
|
|
|
|
|
/** Base class for an object that can be queued for execution in the background.
|
|
|
|
|
|
*
|
2017-12-28 07:23:20 +01:00
|
|
|
|
* It is meaned for long running processes the end user might want to abort,
|
|
|
|
|
|
* pause and check progress on.
|
2017-10-05 16:00:16 +02:00
|
|
|
|
*/
|
2017-12-28 07:23:20 +01:00
|
|
|
|
class QueuedBackgroundTask: public QRunnable {
|
|
|
|
|
|
public:
|
|
|
|
|
|
using CompletionFunction = std::function<void(QRunnable *)>;
|
|
|
|
|
|
|
|
|
|
|
|
explicit QueuedBackgroundTask(CompletionFunction on_completion);
|
|
|
|
|
|
virtual ~QueuedBackgroundTask();
|
|
|
|
|
|
|
|
|
|
|
|
void cancel();
|
|
|
|
|
|
void wait();
|
|
|
|
|
|
bool hasException() const;
|
|
|
|
|
|
void rethrow();
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
bool m_canceled = false;
|
|
|
|
|
|
bool m_started = false;
|
|
|
|
|
|
bool m_finished = false;
|
|
|
|
|
|
std::exception_ptr doRunException = nullptr;
|
|
|
|
|
|
CompletionFunction m_completionFunction = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
QMutex stateMutex;
|
|
|
|
|
|
QWaitCondition finished;
|
|
|
|
|
|
|
|
|
|
|
|
virtual void run() override;
|
|
|
|
|
|
|
|
|
|
|
|
virtual void doRun() = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// /** Task should exit it's run method but remember it's state, it might be resumed later.
|
|
|
|
|
|
// */
|
|
|
|
|
|
// void requestPause();
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
// /** Request to stop running and forget progress
|
|
|
|
|
|
// */
|
|
|
|
|
|
// void requestAbort();
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
// QString getProgressInfo();
|
2017-10-05 16:00:16 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
//class BackgroundTaskInfo {
|
|
|
|
|
|
//public:
|
|
|
|
|
|
//};
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
///** Manages objects of type QueuedBackgroundTask
|
|
|
|
|
|
// *
|
|
|
|
|
|
// * The basic operation of this queue is hand everything directly of to the default
|
|
|
|
|
|
// * threadpool and that will decide when to run each task. But extra functions are
|
|
|
|
|
|
// * provided to control tasks to allow the program to have a UI for controlling the tasks
|
|
|
|
|
|
// */
|
|
|
|
|
|
//class BackgroundTaskQueue {
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
//public:
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
// /** Returns a list of tasks in the queue and their progress.
|
|
|
|
|
|
// *
|
|
|
|
|
|
// * When building a UI to show realtime activity then make sure to first register the update events
|
|
|
|
|
|
// * then get the snapshot. After that you can use the events to update the contents of the UI.
|
|
|
|
|
|
// */
|
|
|
|
|
|
// QVector<BackgroundTaskInfo> getTaskInfoSnapshort();
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
// void pauseTask( );
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
// /** Resume a paused task
|
|
|
|
|
|
// */
|
|
|
|
|
|
// void resumeTask( );
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
// void abortTask( );
|
2017-10-05 16:00:16 +02:00
|
|
|
|
|
2017-12-28 07:23:20 +01:00
|
|
|
|
//};
|