等待 QProcess 完成或持续时间超过 [英] Waiting for QProcess to finish or duration to exceed

查看:154
本文介绍了等待 QProcess 完成或持续时间超过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对以下情况寻找解决方案:

I am trying to find a solution to the following situation:

QProcess 应该运行命令并在达到所需的持续时间或命令完成时停止执行它.这个 QProcess 是由一个 QThread Worker 启动的.

A QProcess should run a command and stop executing it when a desired duration is reached or the command is finished. This QProcess is started by a QThread Worker.

使用这个可以很容易地做到这一点:

This could easily be done by using this:

QProcess task("executedTool -parameters");
task.start();
task.waitForFinished(desired_max_duration_in_ms);

但也必须有暂停洞东西的功能.我可以通过 pthread 信号轻松暂停 QProcess:

But there must also be a function to pause the hole thing. I can easily pause the QProcess via pthread signals:

kill(task_.pid(), SIGSTOP);

kill(task_.pid(), SIGCONT);

但是当 QProcess 暂停时,waitForFinished-duration 继续运行并超过.

But then the waitForFinished-duration continues running and exceeds while the QProcess is paused.

有人知道如何做到这一点吗?当我可以暂停运行 QProcess 和等待命令的孔 QThread 时,等待持续时间也会暂停.但是如何获得 QThread 的 pid 呢?是否有更好的 Qt 解决方案来执行 QProcess 暂停?

Does anybody have an idea how to do this? When I could pause the hole QThread, which runs the QProcess and the wait Command, the wait duration would also be paused. But how can I get the pid of a QThread? Is there a nicer Qt solution to do the QProcess pausing?

推荐答案

自旋锁是一个简单而简单的解决方案.我用 while 循环替换了 waitForFinished(desired_duration),每个循环休眠 100 毫秒.此循环的条件是系统时间小于开始时的系统时间加上所需的持续时间.当 paused_ 变量设置时,100ms 将被添加到所需的持续时间,因此它会不断推送测量的时间,但也会推送所需的持续时间.

An easy and simple solution are Spinlocks. I replaced the waitForFinished(desired_duration) with a while loop, sleeping 100ms each loop. The condition for this loop is that the system time is less then the system time at the start, plus the desired duration. When the paused_ variable is set 100ms will be added to the desired duration, so it keeps pushing the measured time, but also the desired duration.

qint64 ms_start = QDateTime::currentMSecsSinceEpoch();
qint64 ms_end = ms_start + (bag_list_->at(i)->getDuration() * 1000) + 1500;

while (QDateTime::currentMSecsSinceEpoch() < ms_end && worker_thread_->isRunning())
{
  SleepThread::msleep(100);
  if (paused_)
  {
    qDebug() << "add 100";
    ms_end += 100;
  }
}

这可能不是一个好的解决方案,但目前它正在起作用.

That probably isn't a nice solution, but it's working for the moment.

如果有人有其他想法,我很乐意听到.

If somebody has other ideas I would be happy to hear them.

这篇关于等待 QProcess 完成或持续时间超过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆