Results 1 to 15 of 15

Thread: QThread help please

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default QThread help please

    Hi,

    I have to call a php file with POST request simultaneoulsy for some n times. For this I am using QThread and QHttp. Here is the code.

    The thread part

    Qt Code:
    1. for(int i = 0;i < noOfThreads; i++)
    2. {
    3. MyThread *t = new MyThread();
    4. connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
    5. t->run();
    6. }
    To copy to clipboard, switch view to plain text mode 

    The Http Part

    Qt Code:
    1. op->clearPendingRequests();
    2. for(int i=0;i<n;i++) {
    3. QString log = GenerateLog();
    4. QHttpRequestHeader header( "POST", "/somephpfile.php" ) ;
    5. header.setValue( "Host", ipAddress ) ;
    6. header.setContentType( "application/x-www-form-urlencoded" ) ;
    7. http.request(header,QVariant(log).toByteArray());
    8. }
    To copy to clipboard, switch view to plain text mode 

    The above code is not working. Can someone please help?

    Thanks.
    Oh by the way here is my run () function

    Qt Code:
    1. void MyThread::run()
    2. {
    3. mutex.lock();
    4. Http list(ipAddress, n);
    5. list.SetHost(ipAddress);
    6. list.info();
    7. mutex.unlock();
    8. }
    To copy to clipboard, switch view to plain text mode 

    I am new to both threading and http stuff. It would be great if some one can suggest some tutorial or help.

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread help please

    Where is the mutex defined?

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    mutex is private member variable of the class MyThread

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread help please

    You should call t->start() not t->run().

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    i have tried both, but its not working.

    Someone please Help.

    Thanks a lot

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread help please

    I'm not sure but maybe QHttp needs an event loop running? What happens if you call exec() at the end of run()?

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    Yes, it works but i have another problem now.

    I want to post the request in a loop. For that i wrote

    Qt Code:
    1. for(int i=0;i<noOfSeconds;i++)
    2. {
    3. t= QTime::currentTime();
    4. t.addMSecs(1000);
    5.  
    6. for(int j=0;j<logsPerSecond;j++)
    7. {
    8. Http list();
    9. list.SetHost(ipAddress);
    10. list.GenerateLog();
    11. exec();
    12.  
    13. t1 = QTime::currentTime();
    14. if(t1 > t){
    15. break;
    16. }
    17. }
    18. t1 = QTime::currentTime();
    19. while(t1 < t){
    20. t1= QTime::currentTime();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    The above code post the request only once, where as it should have posted more than once.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread help please

    Use a timer and in its timeout slot make a new request.

  9. #9
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    Let me explain it to you in detail.

    I have to generate log for n seconds at the rate of m logs per sec. All this should happned in a thread (Let us assume that there are x threads).

    Following is my code for it.

    x threads are created in a for loop

    Qt Code:
    1. for(int i = 0; i < x; ++i){
    2. MyThread *t = new MyThread(ipEdit->text(), serialNo, noOfSecsEdit->value(), noOfSecondEdit->value());
    3. connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
    4. t->start();
    5. }
    To copy to clipboard, switch view to plain text mode 

    The run()

    Qt Code:
    1. void MyThread::run()
    2. {
    3. QTime t, t1;
    4. for(int i=0;i<noOfSeconds;i++)
    5. {
    6. t= QTime::currentTime();
    7. t.addMSecs(1000);// to know what will be the time after one sec
    8.  
    9. for(int j=0;j<logsPerSecond;j++)
    10. {
    11. Http list();
    12. list.SetHost(ipAddress);
    13. list.GenerateLog();
    14. exec();
    15. t1 = QTime::currentTime();
    16. if(t1 > t){// if one sec is over then come out of the loop
    17. break;
    18. }
    19. }
    20.  
    21. t1 = QTime::currentTime();
    22.  
    23. while(t1 < t){//Wait till one sec is finished.
    24. t1= QTime::currentTime();
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    Notice that i am not using mutex any more. With the above code log is generated only once.

    Can you tell me why is it generated only once whereas it should have got generated more than once ?

    Also can you tell me more about even loop and exec() ?

    Thanks a lot.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QThread help please

    Quote Originally Posted by munna
    Qt Code:
    1. void MyThread::run()
    2. {
    3. ...
    4. for(int j=0;j<logsPerSecond;j++)
    5. {
    6. Http list();
    7. list.SetHost(ipAddress);
    8. ...
    9. exec();
    10. ...
    11. }
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 
    Is this a real code? IMO it shouldn't even compile.

    QThread::exec() is a blocking call and returns after you invoke QThread::exit(). Since you don't invoke it anywhere, only a half of the first iteration of the above loop is executed.

    Quote Originally Posted by munna
    Also can you tell me more about even loop and exec() ?
    QThread::exec() starts an event loop in the current thread (just like QCoreApplication::exec() or QDialog::exec()). This way QObjects that live in that thread can receive events.

    What is that Http class? Does it use QHttp internally? Are you aware that QHttp works asynchronously?

  11. #11
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    Quote Originally Posted by jacek
    Is this a real code? IMO it shouldn't even compile.
    Yes this is the real code. I comiples and works, but not as expected.

    Quote Originally Posted by jacek
    QThread::exec() is a blocking call and returns after you invoke QThread::exit().
    I am new to all this. can you please explain this in more detail or some tutorial.

    Quote Originally Posted by jacek
    What is that Http class? Does it use QHttp internally?
    Yes, the http class inherits QHttp.

    Quote Originally Posted by jacek
    Are you aware that QHttp works asynchronously?
    Not before you told. But I do not understand this stuff completely. Can you please explain or refer to some tutorial. I'll be greatful to you.

    Thanks a lot. Thanks for your time

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QThread help please

    Quote Originally Posted by munna
    Yes this is the real code. I comiples and works, but not as expected.
    That's quite interesting. What compiler do you use? Can you compile this program with it?
    Qt Code:
    1. class Test
    2. {
    3. public:
    4. Test() {};
    5. void foo() {};
    6. };
    7.  
    8. int main()
    9. {
    10. Test t();
    11. t.foo();
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by munna
    I am new to all this. can you please explain this in more detail or some tutorial.
    Everything you need is in the docs --- just read them carefully. Also check the network/http example.

    Your MyThread::run() should look like this:
    Qt Code:
    1. void MyThread::run()
    2. {
    3. HttpLogger logger;
    4. connect( &logger, SIGNAL( finished() ), this, SLOT( quit() ) );
    5. logger.start();
    6. exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    The rest should be handled by signals & slots of the logger object (or whatever you will call it). HttpLogger::start() should start the timer and after given interval that object should emit finished() signal to stop the thread.

  13. #13
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    can i know what makes you think that this code should not compile ?

    I'll look into everything you have suggested and then probably get back.

    Thanks a lot.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QThread help please

    Quote Originally Posted by munna
    can i know what makes you think that this code should not compile ?
    This:
    Qt Code:
    1. Http list();
    To copy to clipboard, switch view to plain text mode 
    it's a forward declaration of a function, but you use "list" like a variable.

  15. #15
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: QThread help please

    oh, I get your point.

    Actually the line was

    Qt Code:
    1. Http list(few parameters);
    To copy to clipboard, switch view to plain text mode 

    but I removed them to make the example look simple.

    Sorry, It was my mistake.

    Thanks for your time and advice.
    Thanks a lot.

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 03:27
  2. Replies: 4
    Last Post: 10th May 2006, 22:37
  3. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51
  4. QThread and heap
    By paranoid_android in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 10:13
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.