Application don` t exit (maybe it cause qfuture?)
-
I'm writing my own messenger, and after updating the frontend to qml, I noticed that the application no longer terminates when the window is closed, although I even explicitly set setQuitOnLastWindowClosed in QGuiApplication. After I added an onClosing slot to the ApplicationWindow type qml, and associated the quit signal QQmlApplicationEngine with QGuiApplication::quit, this did not give any result, then I associated this signal with the lambda, through qDebug I saw that it was called, and that qApp->quit() was called also, but does not give a way out. Then I used std::exit, everything worked, but the QWaitCondition: Destroyed while threads are still waiting log appeared.
In general, as I understand it, QtConcurrent does not allow the application to close, which is necessary for an asynchronous connection with the server. I don't want to use std::exit, it's still probably not safe, how can I ignore the threads in the application and close them with quit? exit doesn't work either, by the way.
-
@qtenjoyer How exactly do you use threads?
You should stop your threads when your application is closing. -
@jsulm
I have a Connection class, inside of which there is a restoreConnection method, and it already uses QtConcurrent::run to asynchronously establish a connection to the server. Below is the method codevoid Connection::restoreConnection() noexcept { if (m_socket) m_socket->deleteLater(); QFuture<void> future = QtConcurrent::run([this]() { m_socket = new QTcpSocket(nullptr); while (true) { m_socket->connectToHost(ip::ServerIp, ip::ServerPort); if (m_socket->waitForConnected()) { qDebug() << "Succesfull connected to server"; AbstractNetworkSocket::initializeFromSocket(m_socket); if (isAuthenticated()) { auto request = outgoingRequestFlags( PacketType::Login, this, PacketFlags(PacketFlags::AuthorizationRequest), << getUserId() << utils::makeSHA256Hash(getPassword()) ); request->onCompleted([this](OutgoingRequestProxy& proxy) { QUuid token; proxy >> token; setToken(token); emit authentificated(); }); request->onError(replyes::incorrentLogin,[this]() -> void { emit invalidAuthentificate(); }); request->onError([request](ReplyStatus status) -> void { request->tryAgain(); }); } return; } qCritical() << "Retry connect to server.."; } }); }
-
@jsulm said in Application don` t exit (maybe it cause qfuture?):
You should stop your threads when your application is closing.
-
@qtenjoyer said in Application don` t exit (maybe it cause qfuture?):
QFuture<void> future = QtConcurrent::run(this {
Try to call https://doc.qt.io/qt-6/qfuture.html#cancel on that future if it is still running (when you close your app).
-
@jsulm
you can’t cancel qfuture that was launched via qtconcurrent::run, I ended up doing without using a thread at all, I just did connectToHost in restoreConnection without waiting, connected onConnect for the socket and processed all the logic there