#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <csignal>
class WorkerThread
: public QThread{
public:
explicit WorkerThread
(QObject *parent
= 0) :m_bStopThread(false) { }
private:
void run()
{
// Thread run forever or until m_bStopThread = true
while (!m_bStopThread)
{
qDebug() << "Worker thread operation... " << currentThreadId();
}
return; // This breakpoint will never be triggered once the application is stop using X button of the windows or CTRL+C keyboard keys.
}
public :
volatile bool m_bStopThread;
public Q_SLOTS:
void aboutToQuitApp()
{
m_bStopThread = true; // This breakpoint will never be triggered once the application is stop using X button of the windows or CTRL+C keyboard keys.
}
};
// This struct is a tips that I get from a forum that is supposed to catch the application closing. But it does not really work at all.
struct CleanExit
{
static WorkerThread* pWorkerThread;
CleanExit(WorkerThread* _pWorkerThread)
{
CleanExit::pWorkerThread = _pWorkerThread;
signal(SIGINT, &CleanExit::exitQt);
signal(SIGTERM, &CleanExit::exitQt);
signal(SIGBREAK, &CleanExit::exitQt) ;
}
static void exitQt(int sig)
{
// This breakpoint will be triggered only while using CTRL+C to close the application.
// But the debugger loses his way as soon as I step further...
// It is as if the application was already being closed and that I have no control on the
// worker thread. The system has kill the thread because its parent process has been killed.
pWorkerThread->m_bStopThread = true;
pWorkerThread->wait();
}
};
WorkerThread* CleanExit::pWorkerThread = NULL;
int main(int argc, char *argv[])
{
WorkerThread wk;
CleanExit cleanExit(&wk);
QObject::connect(&wk,
SIGNAL(finished
()),
&a,
SLOT(quit
()));
// The signal aboutToQuit [B]does not seem to work using Console Application[/B].
QObject::connect(&a,
SIGNAL(aboutToQuit
()),
&wk,
SLOT(aboutToQuitApp
()));
qDebug
() <<
"Starting worker thread ..." <<
QThread::currentThreadId();
wk.start();
// Wait until the worker thread has finished his work !
wk.wait();
// Close the console application
return a.quit();
}
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <csignal>
class WorkerThread : public QThread
{
public:
explicit WorkerThread (QObject *parent = 0)
:m_bStopThread(false) { }
private:
void run()
{
// Thread run forever or until m_bStopThread = true
while (!m_bStopThread)
{
qDebug() << "Worker thread operation... " << currentThreadId();
QThread::msleep(1000);
}
return; // This breakpoint will never be triggered once the application is stop using X button of the windows or CTRL+C keyboard keys.
}
public :
volatile bool m_bStopThread;
public Q_SLOTS:
void aboutToQuitApp()
{
m_bStopThread = true; // This breakpoint will never be triggered once the application is stop using X button of the windows or CTRL+C keyboard keys.
}
};
// This struct is a tips that I get from a forum that is supposed to catch the application closing. But it does not really work at all.
struct CleanExit
{
static WorkerThread* pWorkerThread;
CleanExit(WorkerThread* _pWorkerThread)
{
CleanExit::pWorkerThread = _pWorkerThread;
signal(SIGINT, &CleanExit::exitQt);
signal(SIGTERM, &CleanExit::exitQt);
signal(SIGBREAK, &CleanExit::exitQt) ;
}
static void exitQt(int sig)
{
// This breakpoint will be triggered only while using CTRL+C to close the application.
// But the debugger loses his way as soon as I step further...
// It is as if the application was already being closed and that I have no control on the
// worker thread. The system has kill the thread because its parent process has been killed.
pWorkerThread->m_bStopThread = true;
pWorkerThread->wait();
QCoreApplication::exit(0);
}
};
WorkerThread* CleanExit::pWorkerThread = NULL;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
WorkerThread wk;
CleanExit cleanExit(&wk);
QObject::connect(&wk, SIGNAL(finished()), &a, SLOT(quit()));
// The signal aboutToQuit [B]does not seem to work using Console Application[/B].
QObject::connect(&a, SIGNAL(aboutToQuit()), &wk, SLOT(aboutToQuitApp()));
qDebug() <<"Starting worker thread ..." << QThread::currentThreadId();
wk.start();
// Wait until the worker thread has finished his work !
wk.wait();
// Close the console application
return a.quit();
}
To copy to clipboard, switch view to plain text mode
Bookmarks