How to stop a QThread inmediately?
-
This is mi case, a litte code o mi program
@
class Transaction:public QThread
{
Q_OBJECT
public:
Transaction(QObject *parent):QThread(parent)
{
//Any initialization of data
}
protected:void run()
{
//Connection with serial port and socket connect (QtcpSocket.connectToHost()
//QtcpSocket waitForConnect
//Operation I/O blockink (Serial Port Read)
while(true)
{
//Data analice and generation dato for server
//Operation blocking QtcpWrite (waitForBytesWritten
//Operation blocking, QtcpSocket (waitForReadyRead)
//Data analice and generate date for serial port or finished
if( fin)
break;
//Operation I/O blocking (serial Port write)
//Operation I/O blockink (Serial Port Read)}
}
class MainMenu:public QWidget
{
Q_OBJECT
Transaction transaction;
MainMenu()
{
connect(ui.startTransaction,SIGNAL(clicked()),this,SLOT(start()));
//Show a screen waiting connect(ui.stopTransaction,SIGNAL(clicked()),this,SLOT(stop()));
}
protected slots:
void start()
{
transaction.start();
}void start()
{
transaction.setTerminationEnabled(true);
transaction.terminate();
}}
@under linux it works perfectly, under windows the application crash at runtime, the aplication dead. Whe append this secuencie.
1- start
2- stop while any operation blocking is apeend
3- start
The aplication crash in the call start() of metod QThread -
This is my standard approach to QThread in all platforms:
@
QThNetHTTPServerConn::QThNetHTTPServerConn() {
CanFree= false;
}QThNetHTTPServerConn::~QThNetHTTPServerConn() {
CanFree= true;
while (this->isRunning()) msleep(10);
}void QThNetHTTPServerConn::run() {
while (!CanFree) msleep(100);
}
@If you want to restart the same QThread please set CanFree= true; and then the QThread has ends the run function (use an event to know when) please run start() again.
-
But, it will not terminate inmediately. It will wait for all blocking operation on the socket and on the serial port until evaluate the condition. Consecuently the thread may be delay a lot of seconds.
-
Is not possible, in Qt as Delphi or Freepascal.
As is not possible to not freeze the main form if you click on a button that runs WaitForreadyRead() -
then, whats is the solution on Qt?? Is the last solution do the I/O operations non blocking, implementing a state maching??
-
I use often QThread. When I have to stop it, I normally wait till the thread is stopped....
What are you doing exactly? -
Terminate the thread in any point, everywhere it is not important.
the problem is: I have only one hardware available, which is accesible by serial port, I want to stop the thread inmediately for leave the hardware free for other operation. Even if the user cancel the operation, and the operation in the serial port are blocking becouse it is more easy of managing. -
It is a typical situation for me.
In this case you have to verify frequently the CanFree variable in order to stop in less than a second and release the device.
Why you have to use the same device with more software? -
No, I use the same device with the same software, but in diferents situacions, and diferents operations.
Maybe my English is no not enought precise, Sorry about that. -
If you use the a device with only one program you have to think to the QThread as a component that obscures the device and works as provider for the program.
You have to implement a public function SendCommand(), called from forms etc., the QThread has to run each operation and send a feedback to the program using signals.