The MainWindow now uses a background task to load the catalog.
As the background task can't be cancelled yet only closing of the window can block if the task is still running.
This commit is contained in:
parent
057e745ebe
commit
2705a3417b
4 changed files with 173 additions and 64 deletions
|
|
@ -1,52 +1,81 @@
|
|||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <QRunnable>
|
||||
#include <QVector>
|
||||
//#include <QVector>
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
|
||||
/** Base class for an object that can be queued for execution in the background.
|
||||
*
|
||||
* It is meaned for long running processes the end user might want to abort, pause and check progress on.
|
||||
* It is meaned for long running processes the end user might want to abort,
|
||||
* pause and check progress on.
|
||||
*/
|
||||
class QueuedBackgroundTask: public QRunnable
|
||||
{
|
||||
/** Task should exit it's run method but remember it's state, it might be resumed later.
|
||||
*/
|
||||
void requestPause();
|
||||
|
||||
/** Request to stop running and forget progress
|
||||
*/
|
||||
void requestAbort();
|
||||
|
||||
QString getProgressInfo();
|
||||
};
|
||||
|
||||
class BackgroundTaskInfo {
|
||||
public:
|
||||
};
|
||||
|
||||
/** 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 {
|
||||
|
||||
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();
|
||||
|
||||
/** 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();
|
||||
|
||||
void pauseTask( );
|
||||
|
||||
/** Resume a paused task
|
||||
*/
|
||||
void resumeTask( );
|
||||
|
||||
void abortTask( );
|
||||
// /** Request to stop running and forget progress
|
||||
// */
|
||||
// void requestAbort();
|
||||
|
||||
// QString getProgressInfo();
|
||||
};
|
||||
|
||||
//class BackgroundTaskInfo {
|
||||
//public:
|
||||
//};
|
||||
|
||||
///** 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 {
|
||||
|
||||
//public:
|
||||
|
||||
// /** 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();
|
||||
|
||||
// void pauseTask( );
|
||||
|
||||
// /** Resume a paused task
|
||||
// */
|
||||
// void resumeTask( );
|
||||
|
||||
// void abortTask( );
|
||||
|
||||
//};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue