mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 18:55:38 +08:00
[FSD] Thread utility function to "call function in its own thread"
This commit is contained in:
committed by
Mat Sutcliffe
parent
928a76a042
commit
f620147fe6
@@ -12,6 +12,8 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
#include <QPointer>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
@@ -87,4 +89,18 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
return threadInfo(QThread::currentThread());
|
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
|
} // ns
|
||||||
|
|||||||
@@ -12,7 +12,9 @@
|
|||||||
#define BLACKMISC_CTHREADUTILS_H
|
#define BLACKMISC_CTHREADUTILS_H
|
||||||
|
|
||||||
#include "blackmisc/blackmiscexport.h"
|
#include "blackmisc/blackmiscexport.h"
|
||||||
|
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
@@ -52,6 +54,9 @@ namespace BlackMisc
|
|||||||
|
|
||||||
//! Info about current thread
|
//! Info about current thread
|
||||||
static const QString currentThreadInfo();
|
static const QString currentThreadInfo();
|
||||||
|
|
||||||
|
//! Call in object's thread IF not already in object's thread
|
||||||
|
static bool callInObjectThread(QObject *object, std::function<void()> callFunct);
|
||||||
};
|
};
|
||||||
} // ns
|
} // ns
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user