[FSD] Thread utility function to "call function in its own thread"

This commit is contained in:
Klaus Basan
2019-11-19 19:12:35 +01:00
committed by Mat Sutcliffe
parent 928a76a042
commit f620147fe6
2 changed files with 21 additions and 0 deletions

View File

@@ -12,6 +12,8 @@
#include <QObject>
#include <QThread>
#include <QtGlobal>
#include <QPointer>
#include <QTimer>
namespace BlackMisc
{
@@ -87,4 +89,18 @@ namespace BlackMisc
{
return threadInfo(QThread::currentThread());
}
bool CThreadUtils::callInObjectThread(QObject *object, std::function<void()> callFunct)
{
if (!object) { return false; }
if (CThreadUtils::isCurrentThreadObjectThread(object)) { return false; }
QPointer<QObject> myself(object);
QTimer::singleShot(0, object, [ = ]
{
if (!myself) { return; }
callFunct();
});
return true;
}
} // ns