我如何终止延迟/等待条件
我想知道是否有任何方法可以终止等待/延迟条件。
我正在使用 QTest::qwait(ms)
在我的代码中添加响应延迟。现在我想终止/打破这个延迟。例如,QTest::qWait(2000)
将开始 2 秒的延迟,那么我应该做什么来终止这 2 秒的等待时间?
注意:QTimer
不适合我的代码,我正在使用 Qtest:qwait
来添加延迟。
I would like to know if there is any way to terminate a wait / delay condition.
I am using QTest::qwait(ms)
for adding responsive delay in my code. Now i would like to terminate/break this delay. Something like, QTest::qWait(2000)
will start a 2 sec delay, so what should i do to terminate this 2 second wait time?
Note: QTimer
is not suitable for my code and i am using Qtest:qwait
for adding the delay.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案:你不能。问题是,即使您使用 QTimer,假设 QTimer 超时应该停止等待时间,您会将超时信号连接到什么?或者连接的超时槽会执行什么或者它将调用哪个函数来停止等待?
最好的选择是使用静态方法
QThread::currentThread
来获取指向当前 QThread 的指针,然后您可以使用该指针来强加等待条件QThread::wait(2000)
然后您可以使用外部线程在有条件时停止它。让我们举一个例子,您希望一个线程等待 2 秒或直到进程递增到计数器直到 9999999999。在这种情况下,首先您需要创建自己的类,然后在代码中使用它:在您的实现中:
我知道你需要用测试方法来做到这一点,但据我所知,我想不出其他方法。希望它有帮助:)
The simple answer: You can't. The problem is that even if you use QTimer, and let's say a timeout of the QTimer is supposed to stop the waiting time, what would you connect your timeout signal to? Or what would a connected timeout slot execute or which function would it call to stop the wait?
Your best bet is to use the static method
QThread::currentThread
to obtain a pointer to the current QThread which you can then use to impose a wait condition on by usingQThread::wait(2000)
and then you can use an external thread to stop it on a condition. Let's take an example where in you want a thread to wait for 2s or till a processes increments to a counter till 9999999999. In that case, first you need to create your own class and then use it in your code:And in your implementation:
I understand that you need to do this with the testing method, but as far as I go, I can't think of another way. Hope it helps :)