2017-01-04 15:12:31 +01:00
|
|
|
#ifndef ASYNCEXEC_H
|
|
|
|
#define ASYNCEXEC_H
|
|
|
|
|
|
|
|
#include "qcoreapplication.h"
|
|
|
|
|
2017-01-18 04:52:47 +01:00
|
|
|
#include <QRunnable>
|
|
|
|
#include <QThreadPool>
|
2017-01-18 21:30:23 +01:00
|
|
|
#include <functional>
|
2017-01-18 04:52:47 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
#define async_exec(a) QThreadPool::globalInstance()->start(new LambdaRunnable(a));
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
class LambdaRunnable : public QRunnable {
|
2017-01-18 21:30:23 +01:00
|
|
|
public:
|
|
|
|
LambdaRunnable(std::function<void()> action)
|
|
|
|
{
|
|
|
|
this->action = action;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void run()
|
2017-01-18 21:30:23 +01:00
|
|
|
{
|
|
|
|
this->action();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<void()> action;
|
|
|
|
};
|
2017-01-04 15:12:31 +01:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
#endif // ASYNCEXEC_H
|