Results 1 to 20 of 23

Thread: Multithreading & GUI

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Multithreading & GUI

    Hello,

    I'm trying to develop a Desktop application that will send multiple request to a website and store the response. i want to do the requests in a separate threat so that the GUI will be responsive. I tried to use QNetworkAccessManager with run,qtconcurrent and i was not able to get success in achieving my goal. Can you guys give me some advice or ideas how can i implement multithreading with QNetworkAccessManager

    example algorithm:
    for(int i=0;i<1000;i++)
    {
    //send HTTP request using QNetworkAccessManager

    } // should not affect the gui !!!!!!

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Multithreading & GUI

    Have you tried to use QTimer with 0 interval ?
    Qt Code:
    1. for(int i=0;i<1000;i++)
    2. {
    3. //send HTTP request using QNetworkAccessManager
    4.  
    5. } // should not affect the gui !!!!!!
    6.  
    7. // with QTimer:
    8.  
    9. void MyClass::sendRequest(){
    10. // send request using QNAM
    11. ++_requestSent;
    12. if (_requestSent < 1000 && !_abort){
    13. QTimer::singleShot(0,this,SLOT(sendRequest()));
    14. }
    15. }
    16. // will not affect the gui, timeouts will be processed as soon as all gui events have been processed
    To copy to clipboard, switch view to plain text mode 
    Btw. why do you need to send a thousand http requests ?

  3. The following user says thank you to stampede for this useful post:

    qthread (30th July 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Thanks for helping . I need it for doing testing.

    The app crashed with this error message:" Creating pipes for GWakeup: Too many open files".(with interval 500) Is that because of too many network connections?! (i'm testing it against server running in my Virtual machine).

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Multithreading & GUI

    The app crashed with this error message:" Creating pipes for GWakeup: Too many open files".(with interval 500) Is that because of too many network connections?!
    Maybe it is the cause. Try to process next request when previous one finished (connect to QNetworkReply::finished () signal) instead of using a timer.

  6. #5
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by stampede View Post
    Maybe it is the cause. Try to process next request when previous one finished (connect to QNetworkReply::finished () signal) instead of using a timer.
    I want to send multiple requests. I think using 'finished' method will slow down my task.

    Quote Originally Posted by wysota View Post
    Sending network requests does not "affect the GUI". You can safely do them in the main thread. Using multiple threads with QNetworkAccessManager makes little sense.
    Sending network requests wont affect the gui but running for loop will.

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

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    I want to send multiple requests. I think using 'finished' method will slow down my task.
    Why so?

    Sending network requests wont affect the gui but running for loop will.
    What for loop?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    Why so?


    What for loop?
    If i use finished method, the next request have to wait for the previous one,right?!

    I need a loop for sending multiple request,right?!

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

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    If i use finished method, the next request have to wait for the previous one,right?!
    No, of course not. You schedule as many requests as you want and QNetworkAccessManager will execute them when the right time comes. As far as I remember it will be sending four requests at a time.

    I need a loop for sending multiple request,right?!
    No, but having the loop is ok as well. Scheduling 1000 requests should take just a couple of milliseconds.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    No, of course not. You schedule as many requests as you want and QNetworkAccessManager will execute them when the right time comes. As far as I remember it will be sending four requests at a time.



    No, but having the loop is ok as well. Scheduling 1000 requests should take just a couple of milliseconds.


    Ok. Thanks for the info. But how can i prevent the loop from affecting my gui. For some reason, i am not able to run the QNAM in separate thread. Can you give tell me steps or sample snippet for achieving my task

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

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    Ok. Thanks for the info. But how can i prevent the loop from affecting my gui.
    It will not affect your GUI.

    For some reason, i am not able to run the QNAM in separate thread.
    You are probably not running an event loop in that thread.

    Can you give tell me steps or sample snippet for achieving my task
    Sure, just state what the actual task is
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    It will not affect your GUI.


    You are probably not running an event loop in that thread.


    Sure, just state what the actual task is
    I am new to qt. I am not able to find any good resource in multithreading. I just want to do fuzzing on web applictions which i was assigned to do testing.

    Here is exactly what i wanted:
    I need to send more than 1000 or 10,000 requests to a web application without affecting the GUI(i tried to run 10,000 request in main thread, it's affecting the gui, not able to access any other items). so i wanted to run the QNAM &loop in a separate thread or any other way in such a way that it should not affect the other tasks. Can you give me a sample snippet or tutorial links to achieve this

    Thank you

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

    Default Re: Multithreading & GUI

    Sending network requests does not "affect the GUI". You can safely do them in the main thread. Using multiple threads with QNetworkAccessManager makes little sense.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Multithreading
    By havij000 in forum Newbie
    Replies: 22
    Last Post: 21st August 2013, 13:35
  2. When to use Multithreading?
    By "BumbleBee" in forum General Programming
    Replies: 5
    Last Post: 30th April 2011, 18:21
  3. regarding multithreading
    By mohanakrishnan in forum Qt Programming
    Replies: 19
    Last Post: 9th December 2009, 08:21
  4. multithreading
    By mickey in forum General Programming
    Replies: 2
    Last Post: 5th June 2008, 22:01
  5. MultiThreading in Qt
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2006, 19:26

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.