有没有办法检测QT QRunnable对象何时完成?

Jak*_*sen 6 qt multithreading threadpool

有没有办法检测QT QRunnable对象何时完成?(除了在run()方法结束时手动创建一些信令事件.)

Pie*_*igi 5

您可以简单地使用QtConcurrent运行可运行对象,并使用QFuture等待完成。

#include <QtConcurrentRun>

class MyRunnable : public Runnable{
  void run();
}
Run Code Online (Sandbox Code Playgroud)

为了运行并等待它,您可以执行以下操作

//create a new MyRunnable to use
MyRunnable instance;

//run the instance asynchronously
QFuture<void> future = QtConcurrent::run(&instance, &MyRunnable::run);

//wait for the instance completion
future.waitForFinished();
Run Code Online (Sandbox Code Playgroud)

Qtconcurrent :: run将启动一个新线程以在实例上执行方法run(),并立即返回一个跟踪实例进度的QFuture。

但是请注意,使用此解决方案,您有责任在执行期间将实例保留在内存中。


Cal*_*itt 4

可能有,或者你可能需要稍高一点.在QFutureQFutureWatcher类的设计与Qt的工作并行架构,以及QFutureWatcher具有信号时,是看该项目已完成.