Results 1 to 10 of 10

Thread: Not getting proper response in slot connected with QNetworkAccessManager second time

  1. #1
    Join Date
    Aug 2015
    Posts
    5
    Platforms
    Windows

    Default Not getting proper response in slot connected with QNetworkAccessManager second time

    Hi All,

    I am working on BB10 native extension which is written in QT. I have started a thread to execute my network request.

    Qt Code:
    1. void* SignalThread(void* parent) {
    2. TemplateJS *pParent = static_cast<TemplateJS *>(parent);
    3.  
    4. int argc = 0;
    5. char **argv = NULL;
    6. webworks::TemplateNDK *m_signalHandler = new webworks::TemplateNDK(pParent);
    7. m_signalHandler->doNetworkRequest(); //sending network request
    8.  
    9.  
    10. delete m_signalHandler;
    11. return NULL;
    12. }
    13.  
    14.  
    15. bool TemplateJS::StartThread(){
    16.  
    17. pthread_attr_t thread_attr;
    18. pthread_attr_init(&thread_attr);
    19. pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
    20.  
    21. pthread_t m_thread;
    22. pthread_create(&m_thread, &thread_attr, SignalThread, static_cast<void *>(this));
    23. pthread_attr_destroy(&thread_attr);
    24. if (!m_thread) {
    25. return true;
    26. } else {
    27. return false;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    Here is my simple code to start request.

    Qt Code:
    1. void TemplateNDK::doNetworkRequest()
    2. {
    3. _networkAccessManager = new QNetworkAccessManager();
    4.  
    5. QObject::connect(_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this,
    6. SLOT(onRequestFinished(QNetworkReply*)));
    7.  
    8. QNetworkRequest request = QNetworkRequest();
    9. QString inputUrl = QString::fromUtf8(url.c_str());
    10. request.setUrl(QUrl(inputUrl));
    11. QNetworkReply* response = _networkAccessManager->get(request);
    12. }
    To copy to clipboard, switch view to plain text mode 

    When i call startThread() function first time slot returns proper response. But when I call same startThread() function second time it stuck and does not execute slot. QObject::connect is returning true both times.

    If using QTimer to abort request after interval then second time onRequestFinished slot is getting executed and returns value of reply->error() as 5 that is "operation canceled".

    Am i doing something wrong with QCoreApplication::exec().
    Please help.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    Never seen exec() being called without object, you better call exec() on the QCoreApplication object that you created.

    The code should be somewhat like this

    Qt Code:
    1. QCoreApplication app(....);
    2.  
    3. // setup stuff
    4.  
    5. app.exec();
    To copy to clipboard, switch view to plain text mode 

    I wonder what you are doing here though.
    This is obviously intended to be used by a non-Qt application. BB10's app framework "Cascades" is Qt4 based. Kind of a strange conflict.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    5
    Platforms
    Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    This code is being used for BB10 native extension which is not fully cascades app. Referring below URL for implementation:

    https://github.com/haahmad/SignalSlo...gnalSlotJS.cpp

    Tried what you suggested to call exec() function using object. But it seems to be not working. In this case, got response successfully for the first time but app stuck somewhere and does not return even after time out.

    Also tried to connect signal slot with QNetworkReply object instead of QNetworkAccessManager object as suggested in some posts. But this is also not working even for the first time.

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    Quote Originally Posted by BB_Shi View Post
    Tried what you suggested to call exec() function using object. But it seems to be not working. In this case, got response successfully for the first time but app stuck somewhere and does not return even after time out.
    Did you connect anything to the application's quit() slot or call it after you are done with the request?

    Alternatively keep the thread running and send new request into it instead of creating and destroying it every time and having to make sure that only one such thread ever exists at the same time?

    Cheers,
    _

  5. #5
    Join Date
    Aug 2015
    Posts
    5
    Platforms
    Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    I tried to call QCoreApplication::quit() and application terminated at that point.

    I will try alternative method also.

    Thanks.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    QCoreApplication QCoreApplication(argc, argv);
    I hope this is just a bad cut and paste job, because I have no idea what you intend this line of code to do.

  7. #7
    Join Date
    Aug 2015
    Posts
    5
    Platforms
    Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    I am not finding any way to create thread only once and send request in already running thread because the function which is calling pthread_create needs to be called by below function. Calling of this function can't be handled. This function is called every time whenever a button is clicked from javascript application.

    Qt Code:
    1. string TemplateJS::InvokeMethod(const string& command) {
    2. // command appears with parameters following after a space
    3. int index = command.find_first_of(" ");
    4. std::string strCommand = command.substr(0, index);
    5. std::string arg = command.substr(index + 1, command.length());
    6. params = arg;
    7. if (strCommand == "request") {
    8. StartThread();
    9.  
    10. strCommand.append(";");
    11. strCommand.append(command);
    12. return strCommand;
    13. }
    14.  
    15. return "Unknown C++ method";
    16. }
    To copy to clipboard, switch view to plain text mode 

    if I call m_signalHandler->doNetworkRequest(); two times in SignalThread then it's working fine and returns good response. But if I call StartThread() two times in above function, app exits.


    Added after 10 minutes:


    Quote Originally Posted by d_stranz View Post
    I hope this is just a bad cut and paste job, because I have no idea what you intend this line of code to do.
    I have changed it to
    Qt Code:
    1. QCoreApplication app(argc, argv);
    To copy to clipboard, switch view to plain text mode 

    Also according to document since exec() is static function of QCoreApplication so it can be called like: QCoreApplication::exec();

    Please correct me if something is wrong. As I am new to this.
    Last edited by BB_Shi; 7th August 2015 at 14:06.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    Quote Originally Posted by BB_Shi View Post
    I am not finding any way to create thread only once and send request in already running thread because the function which is calling pthread_create needs to be called by below function.
    You would of course only call the setup function once.

    Once the thread and its event loop are running, you have a couple of options to send the requests:
    - QCoreApplication:ostEvent and a custom event sent to your worker object.
    - Or letting the worker object block on a semaphore or wait condition and doing traditional cross-thread data handover.
    - Might even work to do QMetaObject::invokeMethod on the worker object (using a Qt::QueuedConnection type)

    Cheers,
    _

  9. #9
    Join Date
    Aug 2015
    Posts
    5
    Platforms
    Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    I am new to this. Can you please provide any sample code that can help.


    Added after 1 38 minutes:


    Is there any way to find out number of threads running in an application?
    Last edited by BB_Shi; 10th August 2015 at 07:58.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Not getting proper response in slot connected with QNetworkAccessManager second t

    Quote Originally Posted by BB_Shi View Post
    I am new to this. Can you please provide any sample code that can help.
    For the first option create a subclass of QEvent.
    For the second option have a look at QWaitCondition or QSemphore.
    For the third option have a look at QMetaObject::invokeMethod()

    Quote Originally Posted by BB_Shi View Post
    Is there any way to find out number of threads running in an application?
    There might be operating system specific API for this.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 21st November 2014, 09:14
  2. Slot connected to a generic signal
    By Momergil in forum Qt Programming
    Replies: 5
    Last Post: 28th June 2014, 23:42
  3. Replies: 11
    Last Post: 2nd February 2013, 15:39
  4. Replies: 2
    Last Post: 26th August 2011, 09:51
  5. Disconnect slot when another is being connected
    By holst in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2009, 10:49

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.