- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有如下代码进行异步调用:
QMetaObject::invokeMethod(this, "endSelectionHandling", Qt::QueuedConnection);
我想这样修改代码:
QMetaObject::invokeMethod(this, "endSelectionHandling", Qt::QueuedConnection);
// I want to add statements here which depend on the result of the above async call.
// How can I wait for the above async call to finish its jobs?
如何等待 Qt asycn 调用完成它的工作?有没有更好的方法?
最佳答案
在您的问题中,您似乎根本不需要异步调用,因为您在进行异步调用后立即等待结果。
但是,如果中间有一些代码,您可以使用 C++11 的 std::async
异步调用该函数,然后等待其 std::future
无论何时何地,您在完成其他操作后需要它的结果。
这是一个例子:
#include <iostream>
#include <future>
#include <thread>
#include <chrono>
#define LOG() std::cout << __func__ << " : "
void test()
{
LOG() << "IN\n";
using namespace std::chrono_literals;
std::this_thread::sleep_for( 1s );
LOG() << "OUT\n";
}
int main()
{
LOG() << "Calling test()...\n";
auto f = std::async( std::launch::async, test );
LOG() << "Running test()...\n";
// ... ...
// ... You can do other stuff here ...
// ... ...
f.wait(); // Blocking call to wait for the result to be available
LOG() << "Exiting...\n";
return 0;
}
这是输出:
main : Calling test()...
main : Running test()...
test : IN
test : OUT
main : Exiting...
这是实际示例:https://ideone.com/OviYU6
更新:
但是,在 Qt 领域,您可能想要使用 QtConcurrent::run
和 QFuture
以 Qt 方式做事。
例子如下:
#include <QDebug>
#include <QtConcurrent>
#include <QFuture>
#include <QThread>
#define LOG() qDebug() << __func__ << ": "
void test()
{
LOG() << "IN";
QThread::sleep( 1 );
LOG() << "OUT";
}
int main()
{
LOG() << "Calling test()...";
auto f = QtConcurrent::run( test );
LOG() << "Running test()...";
// ... ...
// ... You can do other stuff here ...
// ... ...
f.waitForFinished(); // Blocking call to wait for function to finish
LOG() << "Exiting...";
return 0;
}
这是输出:
main : Calling test()...
main : Running test()...
test : IN
test : OUT
main : Exiting...
关于c++ - Qt 异步调用 : how to run something after an async call has finished its job,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587091/
我有带皮肤的 DNN。我的 head 标签有 runat="server"所以我尝试在 head 标签内添加一个标签 "> 在后面的代码中,我在属性中设置了 var GoogleAPIkey。问题是它
我在 Node.JS 中有一个导出模块 exports.doSomethingImportant= function(req, res) { var id = req.params.id; Demo.
我是 F# 的新手,我一直在阅读 F# for Fun and Profit。在为什么使用 F#? 系列中,有一个 post描述异步代码。我遇到了 Async.StartChild函数,我不明白为什么
File 中有一堆相当方便的方法类,如 ReadAll***/WriteAll***/AppendAll***。 我遇到过很多情况,当我需要它们的异步对应物时,但它们根本不存在。 为什么?有什么陷阱吗
我最近开始做一个 Node 项目,并且一直在使用 async 库。我有点困惑哪个选项会更快。在某些数据上使用 async.map 并获取其结果,或使用 async.each 迭代一组用户并将他们的相应
您好,我正在试用 Springs 异步执行器,发现您可以使用 @Async。我想知道是否有可能在 @Async 中使用 @Async,要求是需要将任务委托(delegate)给 @Async 方法在第
我需要支持取消一个函数,该函数返回一个可以在启动后取消的对象。在我的例子中,requester 类位于我无法修改的第 3 方库中。 actor MyActor { ... func d
假设 asyncSendMsg不返回任何内容,我想在另一个异步块中启动它,但不等待它完成,这之间有什么区别: async { //(...async stuff...) for msg
我想用 Mocha 测试异步代码. 我跟着这个教程testing-promises-with-mocha .最后,它说最好的方法是 async/await。 以下是我的代码,我打算将 setTimeo
正如我有限(甚至错误)的理解,Async.StartImmediate 和 Async.RunSynchronously 在当前线程上启动异步计算。那么这两个功能究竟有什么区别呢?谁能帮忙解释一下?
我有一行使用await fetch() 的代码。我正在使用一些调用 eval("await fetch ...etc...") 的脚本注入(inject),但问题是 await 在执行时不会执行从ev
我正在尝试使用 nodeJS 构建一个网络抓取工具,它在网站的 HTML 中搜索图像,缓存图像源 URL,然后搜索最大尺寸的图像。 我遇到的问题是 deliverLargestImage() 在循环遍
我想结合使用 async.each 和 async.series,但得到了意想不到的结果。 async.each([1, 2], function(item, nloop) { async.s
我的代码有问题吗?我使用 async.eachSeries 但我的结果总是抛出 undefined。 这里是我的代码: async.eachSeries([1,2,3], function(data,
我想在 trait 中编写异步函数,但是因为 async fn in traits 还不被支持,我试图找到等效的方法接口(interface)。这是我在 Rust nightly (2019-01-0
async setMyPhotos() { const newPhotos = await Promise.all(newPhotoPromises); someOtherPromise();
async.js 中 async.each 与 async.every 的区别?似乎两者都相同,只是 async.every 返回结果。纠正我,我错了。 最佳答案 每个异步 .each(coll, i
我正在尝试对一组项目运行 async.each。 对于每个项目,我想运行一个 async.waterfall。请参阅下面的代码。 var ids = [1, 2]; async.each(ids,
我的目标是测试 API 调用,将延迟考虑在内。我的灵感来自 this post . 我设计了一个沙箱,其中模拟 API 需要 1000 毫秒来响应和更改全局变量 result 的值。测试检查 500
async.each 是否作为异步数组迭代工作? async.eachSeries 是否作为同步数组迭代工作?(它实际上等待响应) 我问这些是因为两者都有回调,但 async.each 的工作方式类似
我是一名优秀的程序员,十分优秀!