2013-12-18 136 views
0

我想創建一個線程(HttpWorker),當需要喚醒併發送一個http請求時。我希望這可以在單個線程中完成。我正在使用Qt來實現。線程使用Qt發送http請求

我以爲我會這樣做的方式是有一個類MyHttpWorker,將其移動到另一個線程,連接插槽/信號等。然後在線程啓動時,我會使用QNetworkAccessManager來調用get請求。在發送請求後,我會使用QWaitCondition來暫停線程,並且無論何時我需要發送另一個線程,我都會繼續此線程。

但是,當我暫停httpworker線程時,完全不會調用FinishedSlot。如果我使用該類簡單地調用一個http請求,它將毫無問題地執行。所以這個問題被連接到QWaitCondition(或者只是凍結一般的線程)。

我可以簡單地爲每個請求創建和銷燬一個工作者和線程,但我需要發送大量的http請求,所以我認爲這種方法太耗費了(創建線程並反覆銷燬它們) 。

我感謝任何幫助,我可以得到。

這裏是我的代碼:

MyHttpWorker.h

#include <QNetworkReply> 
#include <QDebug> 
#include <QObject> 
#include <QNetworkAccessManager> 
#include <QThread> 
#include <QWaitCondition> 
#include <QMutex>  

class MyHttpWorker : public QObject 
    { 
     Q_OBJECT 

     QNetworkAccessManager* nam;   
     QMutex syncPause; 
     QWaitCondition pauseCond;   

    public: 
     explicit MyHttpWorker(QObject *parent = 0);   
     void MyWake();    
    public slots:   
     void SetTheThread(QThread* thread); 
     void MyStart();   
     void finishedSlot(QNetworkReply* reply);    
    }; 

MyHttpWorker.cpp

MyHttpWorker::MyHttpWorker(QObject *parent) : 
    QObject(parent) 
{ 
    nam = new QNetworkAccessManager(this); 
    QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));  
} 

void MyHttpWorker::finishedSlot(QNetworkReply* reply) 
{ 
    qDebug() << "Finished"; //This slot is never even reached, when i used QWaitCond... 
    if (reply->error() == QNetworkReply::NoError) 
    { 
     QByteArray bytes = reply->readAll(); 
     QString string(bytes); 
     qDebug() << string; 
    }else 
    { 
     qDebug() << reply->errorString(); 
    } 
    reply->deleteLater(); 
} 

void MyHttpWorker::SetTheThread(QThread* thread){ 
    QObject::connect(thread,SIGNAL(started()),this,SLOT(MyStart())); 
} 

void MyHttpWorker::MyWake(){  
    pauseCond.wakeAll(); 
} 

void MyHttpWorker::MyStart(){  
    qDebug() << "Start" ; 

    while(true){ 

     syncPause.lock();  
     qDebug() << "thread waiting..."; 
     pauseCond.wait(&syncPause); 
     qDebug() << "thread resumed."; 
     syncPause.unlock(); 

     //sending the actual request here 
     QNetworkRequest myRequest; 
     myRequest.setUrl(QUrl("http://www.google.com")); 
     nam->get(myRequest); 



    } 

} 

的main.cpp

#include <QCoreApplication> 
#include <QThread> 
#include <QDebug> 
#include <myhttpworker.h> 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 

    //create the worker, thread and launch it... (worker is waiting by default) 
    MyHttpWorker* worker = new MyHttpWorker;  
    QThread* httpThread = new QThread;  
    worker->SetTheThread(httpThread); 
    worker->moveToThread(httpThread);  
    httpThread->start(); 

    //try and send 5 requests ... 
    for(int i=0;i<5;i++){ 
     qDebug() << "Unpausing";  
     QThread::currentThread()->msleep(1000);  
     worker->MyWake();  
    } 

    return a.exec(); 
} 

回答

2

不創建一個無限循環,但讓即使循環處理:

void MyHttpWorker::MyWake() 
{ 
    QMetaObject::invokeMethod(this,"doSend"); 
    //send to the event loop 
} 

// new slot 
void MyHttpWorker::doSend(){  
    //sending the actual request here 
    QNetworkRequest myRequest; 
    myRequest.setUrl(QUrl("http://www.google.com")); 
    nam->get(myRequest); 
} 
//and remove the myStart and all that synchronisation 

那麼當你想停止它只是發送quit到線程。我建議你也連接線程finished信號的deleteLater插槽MyHttpWorker

+0

它的工作原理,謝謝:)我只是想問你是否可以澄清「我建議你也連接線程的完成信號MyHttpWorker的deleteLater插槽「。你在那裏失去了我。 – Malisak

+0

in setTheThread所以它在線程停止時會被刪除(只有在有人調用'quit'後纔會發生) –

+0

啊,當然了。再次感謝 :) – Malisak