Hi guys, so i'm handling network manager calls in a different thread. When the thread is running and performing a POST, and i close the window, the application is still running in the debugger without stopping (unless i manually press stop button). I think it might have to do with the event loop but i'm not too sure. Can someone help?
here is my POST method:
QNetworkReply
*ApiManager
::DoHttpPost(QNetworkAccessManager
*NetworkManager,
const QUrl &Url,
QString Data
){
QNetworkRequest Request(Url);
QNetworkReply *Reply = NetworkManager->post(Request,Data.toUtf8());
Reply->setProperty("Timeout",false);
//Wait for the reply and add a timeout period
Timer.setSingleShot(true);
Timer.setInterval(10000);
QObject::connect(&Timer,
SIGNAL(timeout
()),
&Loop,
SLOT(quit
()));
QObject::connect(&Timer,
&QTimer
::timeout,
[=]() { Reply
->setProperty
("Timeout",
true);
});
QObject::connect(Reply,
SIGNAL(finished
()),
&Loop,
SLOT(quit
()));
Timer.start();
Loop.exec();
return Reply;
}
QNetworkReply *ApiManager::DoHttpPost(QNetworkAccessManager *NetworkManager, const QUrl &Url, QString Data)
{
QNetworkRequest Request(Url);
QNetworkReply *Reply = NetworkManager->post(Request,Data.toUtf8());
Reply->setProperty("Timeout",false);
//Wait for the reply and add a timeout period
QTimer Timer;
Timer.setSingleShot(true);
Timer.setInterval(10000);
QEventLoop Loop;
QObject::connect(&Timer,SIGNAL(timeout()),&Loop,SLOT(quit()));
QObject::connect(&Timer,&QTimer::timeout,[=]() { Reply->setProperty("Timeout",true); });
QObject::connect(Reply,SIGNAL(finished()),&Loop,SLOT(quit()));
Timer.start();
Loop.exec();
return Reply;
}
To copy to clipboard, switch view to plain text mode
here is where i make my thread:
QThread *Thread
= Thread_Manager.
GetThread();
Worker = new QueueWorker(ItemQueue);
Worker->moveToThread(Thread);
//Connect signals and slots
connect(Thread,SIGNAL(started()),Worker,SLOT(Run()));
connect(Worker.data(),SIGNAL(DeleteQueueItem(QueueItem*)),this,SLOT(DeleteItem(QueueItem*)));
connect(Worker.data(),SIGNAL(Finished()),Thread,SLOT(quit()));
connect(Worker.data(),SIGNAL(Finished()),Worker.data(),SLOT(deleteLater()));
connect(Worker.data(),SIGNAL(AuthUser()),this,SLOT(AuthenticateUser()));
//Make sure not to manually delete the thread, instead send it to the thread manager to delete
connect(Thread,&QThread::finished,[=](){
Thread_Manager.DeleteThread(Thread);
Running = false;
emit PopulateModel();
});
Thread->start();
QThread *Thread = Thread_Manager.GetThread();
Worker = new QueueWorker(ItemQueue);
Worker->moveToThread(Thread);
//Connect signals and slots
connect(Thread,SIGNAL(started()),Worker,SLOT(Run()));
connect(Worker.data(),SIGNAL(DeleteQueueItem(QueueItem*)),this,SLOT(DeleteItem(QueueItem*)));
connect(Worker.data(),SIGNAL(Finished()),Thread,SLOT(quit()));
connect(Worker.data(),SIGNAL(Finished()),Worker.data(),SLOT(deleteLater()));
connect(Worker.data(),SIGNAL(AuthUser()),this,SLOT(AuthenticateUser()));
//Make sure not to manually delete the thread, instead send it to the thread manager to delete
connect(Thread,&QThread::finished,[=](){
Thread_Manager.DeleteThread(Thread);
Running = false;
emit PopulateModel();
});
Thread->start();
To copy to clipboard, switch view to plain text mode
Thread_Manager creates threads and adds them to a list, on deletion of the manager it deletes the threads in the list.
Bookmarks