【发布时间】:2011-08-03 14:48:06
【问题描述】:
我正在使用 QThread,在它的 run 方法中,我有一个计时器调用一个函数,该函数执行一些需要一些时间的繁重操作。通常超过触发计时器的时间间隔(但并非总是如此)。
我需要保护这个方法,只有在它完成之前的工作后才能调用它。
代码如下:
NotificationThread::NotificationThread(QObject *parent)
: QThread(parent),
bWorking(false),
m_timerInterval(0)
{
}
NotificationThread::~NotificationThread()
{
;
}
void NotificationThread::fire()
{
if (!bWorking)
{
m_mutex.lock(); // <-- This is not protection the GetUpdateTime method from invoking over and over.
bWorking = true;
int size = groupsMarkedForUpdate.size();
if (MyApp::getInstance()->GetUpdateTime(batchVectorResult))
{
bWorking = false;
emit UpdateNotifications();
}
m_mutex.unlock();
}
}
void NotificationThread::run()
{
m_NotificationTimer = new QTimer();
connect(m_NotificationTimer,
SIGNAL(timeout()),
this,
SLOT(fire(),
Qt::DirectConnection));
int interval = val.toInt();
m_NotificationTimer->setInterval(3000);
m_NotificationTimer->start();
QThread::exec();
}
// This method is invoked from the main class
void NotificationThread::Execute(const QStringList batchReqList)
{
m_batchReqList = batchReqList;
start();
}
【问题讨论】:
-
您需要重新格式化您的问题。不清楚。
-
不要继承 QThread。 labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong
-
这是遗留代码,我不能不继承它