Enter a loop until signal is emitted
-
Hi, I catch a disconnected signal from my FTP Client, What I want to do is when disconnected signal is emitted, the program should start a loop to try reconnect.
There is also a connected signal so I want to enter a loop to send a request for connection to server until the connection is made.
I Use connect() to catch disconnect signal like this:
QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){ qDebug() << "disconnected"; //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted. });
Thanks for advice.
-
Hi, I catch a disconnected signal from my FTP Client, What I want to do is when disconnected signal is emitted, the program should start a loop to try reconnect.
There is also a connected signal so I want to enter a loop to send a request for connection to server until the connection is made.
I Use connect() to catch disconnect signal like this:
QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){ qDebug() << "disconnected"; //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted. });
Thanks for advice.
@kayakaan02 said in Enter a loop until signal is emitted:
should start a loop to try reconnect.
You would do this via
QEventLoop::exec()
. However I would not do this, you would be making thedisconnected
signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.Send your "request for reconnection" from the slot here by all means, but why wait/block until it has reconnected (which may never even happen) from here? Don't. Let the
disconnected
handler exit after sending the reconnect request, and handle the reconnection/QTcpSocket::connected
signal as when it arrives through a separate slot in code. -
@JonB said in Enter a loop until signal is emitted:
You would do this via
QEventLoop::exec()
. However I would not do this, you would be making thedisconnected
signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.Thanks for the answer, the client program is fully automated relying on the connection so it can be dangerous continuing while there is no connection.
But can it cause the other
connect()
functions to stop working? -
@JonB said in Enter a loop until signal is emitted:
You would do this via
QEventLoop::exec()
. However I would not do this, you would be making thedisconnected
signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.Thanks for the answer, the client program is fully automated relying on the connection so it can be dangerous continuing while there is no connection.
But can it cause the other
connect()
functions to stop working?@kayakaan02 said in Enter a loop until signal is emitted:
so it can be dangerous continuing while there is no connection.
I don't know exactly what you mean by this, but in that case isn't your "client program" just as much at "danger" of this during whatever you choose to implement for
//enter a loop to send connection request until
QTcpSocket::connectedsignal is emitted.
as if you allow it to exit and re-enter the main event loop?
But can it cause the other connect() functions to stop working?
You have not allowed Qt to return from the
disconnected
handler yet in your proposed reconnect code. If there is any "clean up" it would do after emitting thedisconnected
signal (I don't know, but there might be?) that has not been allowed to take place. I don't know but that might "mess up" your attempt to reconnect. -
Hi, I catch a disconnected signal from my FTP Client, What I want to do is when disconnected signal is emitted, the program should start a loop to try reconnect.
There is also a connected signal so I want to enter a loop to send a request for connection to server until the connection is made.
I Use connect() to catch disconnect signal like this:
QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){ qDebug() << "disconnected"; //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted. });
Thanks for advice.
@kayakaan02 said in Enter a loop until signal is emitted:
I Use connect() to catch disconnect signal like this:
QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){qDebug() << "disconnected"; //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
});
As said in the other post - don't do this, fix your code.
/edit: here: https://forum.qt.io/topic/141738/qeventloop-occupy-cpu-would-qwaitcondition-will-release-cpu-and-also-be-waked
-
@kayakaan02 said in Enter a loop until signal is emitted:
should start a loop to try reconnect.
You would do this via
QEventLoop::exec()
. However I would not do this, you would be making thedisconnected
signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.Send your "request for reconnection" from the slot here by all means, but why wait/block until it has reconnected (which may never even happen) from here? Don't. Let the
disconnected
handler exit after sending the reconnect request, and handle the reconnection/QTcpSocket::connected
signal as when it arrives through a separate slot in code.@JonB Sorry for disturbing again but, I couldn't find out how can I do that with
QEventLoop
, All the examples on the internet I found just waits for something to happen but I want to make a loop(also wait a couple of seconds after each iteration for the sake of my CPU) to send requests, and then exit the loop if I catch a signal as I said.It could be too much but can you provide an example on how to do that?
Thanks in advance.
-
@JonB Sorry for disturbing again but, I couldn't find out how can I do that with
QEventLoop
, All the examples on the internet I found just waits for something to happen but I want to make a loop(also wait a couple of seconds after each iteration for the sake of my CPU) to send requests, and then exit the loop if I catch a signal as I said.It could be too much but can you provide an example on how to do that?
Thanks in advance.
@kayakaan02
If you insist on doing things as you show, where you want to//enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
inside lambda you would need something like
QEventLoop loop; QObject::connect(&controlChannel.m_socket, &QTcpSocket::connected, [&loop] () { qDebug() << "connected"; loop.quit(); }); loop.exec();
inside your current
&QTcpSocket::disconnected
lambda.But as I said I don't think you should do it this way, and I don't even know if it would work while still inside
disconnected
slot. I would not put anything inside thedisconnected
slot, I would let that exit, and return to the main Qt event loop, and do your waits/loops/sends/receives via signals/slots from the main event loop processing without trying to "block" or do it inside thedisconnect
slot code. -
No loops! they are not the Qt way...
Your disconnect catch slot should set a qtimer that tries reconnect on expire and keep a counter of how many times you intend to retry the connect timer. No futzing with sub-event-loops.