我如何终止延迟/等待条件

发布于 2024-10-18 17:01:18 字数 247 浏览 6 评论 0原文

我想知道是否有任何方法可以终止等待/延迟条件。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2024-10-25 17:01:18

简单的答案:你不能。问题是,即使您使用 QTimer,假设 QTimer 超时应该停止等待时间,您会将超时信号连接到什么?或者连接的超时槽会执行什么或者它将调用哪个函数来停止等待?

最好的选择是使用静态方法 QThread::currentThread 来获取指向当前 QThread 的指针,然后您可以使用该指针来强加等待条件 QThread::wait(2000) 然后您可以使用外部线程在有条件时停止它。让我们举一个例子,您希望一个线程等待 2 秒或直到进程递增到计数器直到 9999999999。在这种情况下,首先您需要创建自己的类,然后在代码中使用它:

class StopThread : public QThread {
private:
    QThread* _thread;

public:
    StopThread(QThread*);
    void run();
};

StopThread::StopThread(QThread* thread) {
    _thread = thread;
}

void StopThread::run() {
    //Do stuff here and see when a condition arises
    //for a thread to be stopped
    int i = 0;
    while(++i != 9999999999);
    _thread->quit();
}

在您的实现中:

QThread* thread = QThread::currentThread();
StopThread stopThread(thread);
stopThread->exec();
thread->wait(2000);

我知道你需要用测试方法来做到这一点,但据我所知,我想不出其他方法。希望它有帮助:)

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 using QThread::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:

class StopThread : public QThread {
private:
    QThread* _thread;

public:
    StopThread(QThread*);
    void run();
};

StopThread::StopThread(QThread* thread) {
    _thread = thread;
}

void StopThread::run() {
    //Do stuff here and see when a condition arises
    //for a thread to be stopped
    int i = 0;
    while(++i != 9999999999);
    _thread->quit();
}

And in your implementation:

QThread* thread = QThread::currentThread();
StopThread stopThread(thread);
stopThread->exec();
thread->wait(2000);

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文