标题问题可能不是很明确。我在Windows7上使用Qt5。
在某个时刻的线程(QThread)中,在"process()"
函数/方法中,我必须等待"encrypted()"
属于该线程中正在使用的QSslSocket的SIGNAL。另外我想我应该使用一个QTimer并等待一个"timeout()"
SIGNAL,以避免在无限循环中被阻塞...
我现在所拥有的是:
// start processing data
void Worker::process()
{
status = 0;
connect(sslSocket, SIGNAL(encrypted()), this, SLOT(encryptionStarted()));
QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
timer.start(10000);
while(status == 0)
{
QThread::msleep(5);
}
qDebug("Ok, exited loop!");
// other_things here
// .................
// end other_things
emit finished();
}
// slot (for timer)
void Worker::timerTimeout()
{
status = 1;
}
// slot (for SSL socket encryption ready)
void Worker::encryptionStarted()
{
status = 2;
}
好吧,显然这是行不通的。它永远停留在while循环中……
所以,问题是:有没有办法解决这个问题?我如何等待该"encrypted()"
信号,但不能超过-假设10秒-以避免陷入该等待循环/线程中?
您可以使用本地事件循环来等待信号被发出:
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
connect( sslSocket, &QSslSocket::encrypted, &loop, &QEventLoop::quit );
connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
timer.start(msTimeout);
loop.exec();
if(timer.isActive())
qDebug("encrypted");
else
qDebug("timeout");
在这里,它等待直到encrypted
发出或超时到达。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句