Results 1 to 8 of 8

Thread: how to show busy wait when one funtcion is executing

  1. #1
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    86
    Thanks
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default how to show busy wait when one funtcion is executing

    Hi everybody,

    In my application, i am using third party function, that is taking more time for executing(approx 10 sec).
    For this duration i have to show busy wait, mean processing is going on and blocking the rest of the application.

    i have tried QProcessDialog for this but it's not successful.

    QProgressDialog progressDialog("Processing...", "Abort", 0, INT_MAX, this);
    QProgressBar* bar = new QProgressBar(&progressDialog);
    bar->setRange(0, 0);
    bar->setValue(0);
    progressDialog.setBar(bar);

    progressDialog.setMinimumWidth(320);
    progressDialog.setMinimumDuration(0);
    progressDialog.setWindowModality(Qt::WindowModal);
    progressDialog.setValue(0);

    //QCoreApplication:rocessEvents();

    progressDialog.setValue(progressDialog.value() + 1);

    thirdPartyFunction(); // this function taking much time.

    progressDialog.close();

    It's not working, mean it's even not displaying progress dialog, but if i am using QCoreApplication:rocessEvents(); then progress dialog
    is displaying but not updating.
    I have even tried with QThread, but not became success.

    Can you explain me anybody, how to solved this problem ?
    My problem is whenever i am using some long operating function, i have to show busy wait in foreground.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to show busy wait when one funtcion is executing

    Why are you mixing QProgressBar and QProgressDialog, when you can do it just on of them
    following works just as you want.
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8.  
    9. d.setMaximum(50);
    10. d.setValue(10);
    11. d.show();
    12. QApplication::processEvents();
    13.  
    14.  
    15. while(1); //or som other long function
    16.  
    17. d.setValue(50);
    18.  
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

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

    sanjeet (11th November 2011)

  4. #3
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    86
    Thanks
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to show busy wait when one funtcion is executing

    Hi Santosh,
    First of all very thanks to suggesting me.......
    i have mixed progress bar with progress dialog because i have to show "Busy Wait", mean
    something gif image type of thing rotating. If i was not adding progress bar separately then
    i am unable to show busy wait. When i am adding progress bar, setting range(0,0) , then progress
    indicator moving back and forth........ and it seem to "busy wait" something going on in background.

    can you guide me so that i can show busy wait. problem is that function is third party API, we haven't
    code of these function.
    Even i am trying to implement by posix thread or and QThread......... i am not able to find this solution.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to show busy wait when one funtcion is executing

    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class MyThread : public QThread
    5. {
    6. Q_OBJECT
    7. public:
    8. explicit MyThread(QObject *parent = 0)
    9. : QThread(parent){}
    10.  
    11.  
    12. signals:
    13. void dataReceived(const QString& text);
    14.  
    15.  
    16. protected:
    17. void run(void)
    18. {
    19. while(1)
    20. {
    21. int i;
    22. msleep(100); //do some long function here, third party API
    23. emit dataReceived(QString("%1").arg(i++));
    24. }
    25. }
    26. };
    27.  
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QApplication a(argc, argv);
    32.  
    33.  
    34. QPlainTextEdit w;
    35. MyThread thread;
    36.  
    37.  
    38. w.setMaximumBlockCount(1000);
    39. w.connect(&thread, SIGNAL(dataReceived(QString)), &w, SLOT(appendPlainText(QString)));
    40. w.showMaximized();
    41.  
    42.  
    43. thread.start();
    44. thread.moveToThread(&thread);
    45.  
    46.  
    47. return a.exec();
    48. }
    49.  
    50.  
    51. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Santosh Reddy for this useful post:

    sanjeet (14th November 2011)

  7. #5
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    86
    Thanks
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to show busy wait when one funtcion is executing

    Thank you Santosh, for coding,
    it's very beautiful and small code in compare to what i was doing.
    i was using low level POSIX threads.

    But there is still a problem, i have reached on same position, in both way
    either by POSIX thread or by QThread.
    Now both thread running parallely...., even busy wait is showing.

    But problem is my thread execution is not finishing, It's not a problem in thread.
    because that API(third party function) has stopped execution in mead way, after giving
    some output on console.

    I think it's not getting time slot to finish it's execution. When i was trying with POSIX thread,
    i have even used mutex to lock and unlock that section(that API), but when using mutex, it's
    giving strange behaviour, without unlocking that section it's returning to main thread. And
    creating "system -hang" type of situation.

    where as i observed......... now it's problem of thread synchronization, i have to find out mechanism
    for proper thread synchronization either by semaphore or mutex. i am trying but didn't get it.

    Can you tel me it's problem of thread synchronization ? If Yes, then how to sort out either by QThread
    or by POSIX thread.

  8. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to show busy wait when one funtcion is executing

    But problem is my thread execution is not finishing, It's not a problem in thread.
    because that API(third party function) has stopped execution in mead way, after giving
    some output on console.
    As far I understand your problem, you don't have any problem with threads. You have a problem reading the API output.

    It does not matter for thread whether API has finished normally or it has returned due to some internal error. All you need to do is to check the return value of the API and update the GUI accordingly.

    If this is not the case or if I have not understood your problem, please explain the how you are trying to use the API.

  9. The following user says thank you to Santosh Reddy for this useful post:

    sanjeet (16th November 2011)

  10. #7
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    86
    Thanks
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to show busy wait when one funtcion is executing

    Hi Santosh

    Yes you are right, i think too it's not a problem of thread.
    What's happening when that API(third party function) using in my application, it's stuck, mean it's not finishing execution,(stoping the execution in mid-way).
    I have used the API, where you have suggest me in your demo code. And API will return any value when it will finish the execution either, but it's not coming out of that API.

    But when same API, i have tried with only C, in one thread the api is executing and in another thread something printing in console it's working properly. As, API has written in C. I am using (extern "C").

    i am giving you both demo code:

    Qt Code:
    1. C-Code:
    2.  
    3. #include <unistd.h>
    4. #include <sys/types.h>
    5. #include <pthread.h>
    6. #include <stdio.h>
    7. #include <semaphore.h>
    8. #include <string.h>
    9. #include <stdlib.h>
    10.  
    11. void *tfunc(void *arg);
    12. char msg[]="Hai Thread testing";
    13. int run_var=1;
    14. int selection=0;
    15.  
    16. int main()
    17. {
    18. pthread_t thread;
    19. int res,count=0;
    20. void *thr_result;
    21. res=pthread_create(&thread,NULL,tfunc,(void *)msg);
    22. if(res!=0){printf("\n thread failed\n");exit(0);}
    23. while(count < 30)
    24. {
    25. printf("main\n");
    26. count++;
    27. sleep(1);
    28. }
    29. res=pthread_join(thread,&thr_result);
    30. if(res!=0){printf("\n Join failed\n");exit(0);}
    31. return 0;
    32. }
    33. void *tfunc(void *arg)
    34. {
    35. int count=0;
    36. printf("\n running thread %s\n",(char *)arg);
    37. sleep(1);
    38. strcpy(msg,"Exiting");
    39. printf("in thread\n");
    40.  
    41. third_party_function(); // this is my third party API
    42.  
    43. pthread_exit("THank u");
    44. }
    To copy to clipboard, switch view to plain text mode 


    Now my Qt-Application code:
    i am calling qt-thread in one of my slot.

    myclass.h
    Qt Code:
    1. class MyClass : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. MyClass(QWidget *parent = 0);
    6. private:
    7. QPushButton *startButton;
    8. private slots:
    9. int executionSlot();
    10.  
    11. };
    12.  
    13. class MyThread : public QThread
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit MyThread(QObject *parent = 0)
    18. : QThread(parent){}
    19. signals:
    20. void dataReceived();
    21. protected:
    22. void run();
    23. };
    To copy to clipboard, switch view to plain text mode 

    myclass.cpp

    Qt Code:
    1. void MyThread::run()
    2. {
    3. qDebug("thread running now");
    4. while(1)
    5. {
    6. qDebug("In thread....before API");
    7. int ret = third_party_function(); // this is my third party API
    8.  
    9. // If in place of API, i am using this for loop, for debugging, it's working fine
    10. /*
    11. for(int i=0; i<10; i++){
    12.   qDebug("opening modem");
    13.   sleep(1);
    14.   } */
    15.  
    16. //If i am using API, it's not coming here.
    17. qDebug("In thread....after API");
    18.  
    19. emit dataReceived();
    20. break;
    21. }
    22.  
    23. qDebug("thread finishing now");
    24. }
    25.  
    26.  
    27. int MyClass::executionSlot()
    28. {
    29. qDebug("creating prog dlg");
    30.  
    31. prgDialog prgDlg; //This dialog i have created for showing busy wait proccessing, animation type
    32.  
    33. MyThread thread;
    34.  
    35. prgDlg.connect(&thread, SIGNAL(dataReceived()), &prgDlg, SLOT(close()));
    36.  
    37. thread.start();
    38.  
    39. thread.moveToThread(&thread);
    40.  
    41. prgDlg.exec();
    42.  
    43. qDebug("exiting slot...");
    44.  
    45. return 0;
    46. }
    To copy to clipboard, switch view to plain text mode 
    I have not written full code, because it's very big code, i have just written that section of code where i am getting problem. When i am using that for-loop for some time then it's working fine properly, mean processing dialog appear and after finishing that thread, processing dialog will disappear too . But when i am using the API, processing dialog appears, thread started to run too, but when it's reaching the API, it's not coming out of that API and so processing dialog too running infinite.
    But this is okay with c-code.
    If you are getting my problem by this .... can you suggest me where i am doing wrong. Thank you in advance !
    Last edited by sanjeet; 16th November 2011 at 14:03.

  11. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to show busy wait when one funtcion is executing

    I hope API is not depending on anything from the Qt part of application.

    Is API multi-threaded internally? Then refer it's documentation for any initial configuration for it work properly. Try debugging the API if possible.

    It should very trivial for this to work QThread when it already works on pthread. Are you sure it works with pthread (the code you posted)?

Similar Threads

  1. show a rotating circle when running a busy function
    By hoffen in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2011, 09:33
  2. Busy Indicator using QProgressDialog??
    By qtzcute in forum Qt Programming
    Replies: 13
    Last Post: 6th September 2011, 11:31
  3. busy progress bar without thread ?
    By npc in forum Newbie
    Replies: 34
    Last Post: 23rd July 2009, 09:29
  4. QProgressBar busy indicator
    By Michiel in forum Qt Tools
    Replies: 2
    Last Post: 1st August 2007, 16:54
  5. wait copy picture then show it
    By raphaelf in forum Newbie
    Replies: 6
    Last Post: 5th November 2006, 12:09

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.