How to stop a QThread inmediately?
-
Hello,
I have a Qthread running, and inside of this thread, there is a Qtcp connection.
I call waitForReadyRead, then when the data is ready the thread proces the data. This steps are in a while loop. Now, if the user push a button on the gui ( A cancel transaction button), I need close the socket and terminate the thead.
What is the correct way of do this with Qt and QThread?? in Pthread I have pthread_cancel, what is the equivalent in Qthread?? I already use QThread (SetTerminationEnabled and terminate) but this cause a crash program.
Thanks. -
Hi
how is about this
http://www.qtcentre.org/threads/28957-How-to-stop-QThreador that
http://stackoverflow.com/questions/19388207/how-to-stop-thread-qthreadDo you have an event loop?
Cheers J
-
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.
-
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. -
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.