jigglyslime
4th November 2013, 04:47
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
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;
}
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(Queue Item*)),this,SLOT(DeleteItem(QueueItem*)));
connect(Worker.data(),SIGNAL(Finished()),Thread,SL OT(quit()));
connect(Worker.data(),SIGNAL(Finished()),Worker.da ta(),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();
Thread_Manager creates threads and adds them to a list, on deletion of the manager it deletes the threads in the list.
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
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;
}
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(Queue Item*)),this,SLOT(DeleteItem(QueueItem*)));
connect(Worker.data(),SIGNAL(Finished()),Thread,SL OT(quit()));
connect(Worker.data(),SIGNAL(Finished()),Worker.da ta(),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();
Thread_Manager creates threads and adds them to a list, on deletion of the manager it deletes the threads in the list.