Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: QThread exec proplem to stop...

  1. #21
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread exec proplem to stop...

    Quote Originally Posted by patrik08 View Post
    is running ....

    Qt Code:
    1. class DavListDir : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run();
    6. void SetUrl( QUrl u );
    7. signals:
    8. void DataXml(QString);
    9. /*void TimeEnd();*/
    10. private:
    11. QUrl urltogrep;
    12. QString ned;
    13. HTTP_propfind *jobb;
    14. public slots:
    15. void grepdata();
    16.  
    17. };
    18. void DavListDir::grepdata()
    19. {
    20. ned = jobb->GetDAVData();
    21. if (ned.size() > 0) {
    22. emit DataXml(ned);
    23. }
    24. }
    25. void DavListDir::SetUrl( QUrl u )
    26. {
    27. urltogrep = QUrl(u);
    28. jobb = new HTTP_propfind(urltogrep); /* QHttp first setup header .... */
    29. connect(jobb, SIGNAL(done(bool)), this, SLOT(grepdata()));
    30. }
    31. void DavListDir::run()
    32. {
    33. if (!urltogrep.isValid()) {
    34. quit();
    35. return;
    36. }
    37. jobb->start(); /* QHttp start request */
    38. exec();
    39. forever{
    40. if (isFinished()) {
    41. emit grepdata();
    42. }
    43. };
    44. }
    To copy to clipboard, switch view to plain text mode 
    No, not OK.
    isFinished always returns false when called within thread ( especially from run ).

  2. #22
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread exec proplem to stop...

    this run .... && having time out if loop....

    is this better ....?

    Qt Code:
    1. class DavListDir : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run();
    6. void SetUrl( QUrl u );
    7. signals:
    8. void DataXml(QString);
    9. void TimeEnd();
    10. private:
    11. QUrl urltogrep;
    12. QString ned;
    13. HTTP_propfind *jobb;
    14. bool waiting;
    15. int decimalsec;
    16. public slots:
    17. void grepdata();
    18.  
    19. };
    20. void DavListDir::grepdata()
    21. {
    22. ned = jobb->GetDAVData();
    23. if (ned.size() > 0) {
    24. emit DataXml(ned);
    25. waiting = false;
    26. quit();
    27. }
    28. }
    29. void DavListDir::SetUrl( QUrl u )
    30. {
    31. decimalsec = 0;
    32. waiting = true;
    33. urltogrep = QUrl(u);
    34. jobb = new HTTP_propfind(urltogrep); /* QHttp first setup header .... */
    35. connect(jobb, SIGNAL(done(bool)), this, SLOT(grepdata()));
    36. }
    37. void DavListDir::run()
    38. {
    39. if (!urltogrep.isValid()) {
    40. quit();
    41. return;
    42. }
    43. jobb->start(); /* QHttp start request */
    44. exec();
    45.  
    46. while( waiting ) {
    47. decimalsec++;
    48. msleep(100);
    49. /* time out remote server! 2.5 sec .*/
    50. if (decimalsec > 25) {
    51. waiting = false;
    52. ned ="time_out";
    53. emit DataXml(ned);
    54. quit();
    55. }
    56. }
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

  3. #23
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread exec proplem to stop...

    No Patrick.
    Think at exec() as a loop ( actually it is one ) ). This loop exits when you call quit or exit.
    When the loop exits run() will continue with the next instructions - meaning the code that you have after exec().

    So there is no point in calling quit there. You shouldn't call emit either. Just pout the thread to sleep for as long as you have to.
    Qt Code:
    1. while(wainting)
    2. msleep( 200 );
    To copy to clipboard, switch view to plain text mode 
    You set waiting to false with from the GUI thread with a function that does only that : sets waiting to false( make sure "waiting" is volatile bool ). Therefore create another function in your thread that sets waiting to false.
    You won't need grepdata, or anything else.
    In the GUI thread, first call thread->quit(), and then, after some time( you must know when ), set waiting to false by calling that function.

    When it exits, it will emit finished(). In the slot that gets called by finished you must query the thread for "ned". This seems a reasonable solution to me.

  4. #24
    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: QThread exec proplem to stop...

    What does the while(waiting) loop do anyway? If you're spinning in a loop, why not use the one that is handled by exec()? You can use timers if you want to do something periodically. Seems that all you want is to react after 2.5s, so you can use a timer with such timeout and don't bother complicating things too much. And you can always stop the timer if you want to prevent it from timing out.

  5. #25
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread exec proplem to stop...

    I tested this on bad url bad pass and correct param url is running ... on max 2sec. one dir list... one a clean xml or a message error..

    IMO: If troltech group know that the FTP password are visible clear, on place from QFtp QWebdav is born... and a remote dir model... must not write now..

    if you see other code leaks? ...

    Qt Code:
    1. class DavListDir : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run();
    6. void SetUrl( QUrl u );
    7. signals:
    8. void DataXml(QString);
    9. void TimeEnd();
    10. private:
    11. QUrl urltogrep;
    12. QString ned;
    13. HTTP_propfind *jobb;
    14. bool waiting;
    15. int decimalsec;
    16. public slots:
    17. void grepdata();
    18. void httperror();
    19.  
    20. };
    21. void DavListDir::httperror()
    22. {
    23. emit DataXml(jobb->errorString());
    24. jobb->abort();
    25. exit();
    26. }
    27. void DavListDir::grepdata()
    28. {
    29. ned = jobb->GetDAVData();
    30. if (ned.size() > 0) {
    31. emit DataXml(ned);
    32. waiting = false;
    33. exit();
    34. return;
    35. }
    36. QTimer::singleShot(2000, this, SLOT(httperror()));
    37. }
    38. void DavListDir::SetUrl( QUrl u )
    39. {
    40. decimalsec = 0;
    41. waiting = true;
    42. urltogrep = QUrl(u);
    43. jobb = new HTTP_propfind(urltogrep); /* QHttp first setup header .... */
    44. connect(jobb, SIGNAL(done(bool)), this, SLOT(grepdata()));
    45. }
    46. void DavListDir::run()
    47. {
    48. if (!urltogrep.isValid()) {
    49. waiting = false;
    50. ned ="url_invalid";
    51. emit DataXml(ned);
    52. exit();
    53. return;
    54. }
    55. jobb->start(); /* QHttp start request */
    56. exec();
    57. }
    To copy to clipboard, switch view to plain text mode 

  6. #26
    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: QThread exec proplem to stop...

    I think your code is not very well designed, but if it works for you... hey, it's your application.

  7. #27
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread exec proplem to stop...

    Quote Originally Posted by wysota View Post
    I think your code is not very well designed, but if it works for you... hey, it's your application.
    Then pleas tell my more, it would be to better read or designed, my realy job is kitchen chief... not a qt professional. And i stay here to learn... .. I am only a php guru & webmaster.. on QT book from Molkentin or Blanchette dont say nothing how make code better readable .. Or tell me from all here http://code.google.com/hosting/search?q=label:Qt 94 qt projekt which is better designed. tanks.

  8. #28
    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: QThread exec proplem to stop...

    It's not only about readability, but also performance and ease of maintenance. I won't design your code because simply I don't know what you want it to do I can give you hints how to obtain a better design and I think I've already given a few in this thread.

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

    patrik08 (21st May 2007)

  10. #29
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread exec proplem to stop...

    Witold, My actual book are UML design plattern on italian language
    http://www.hoepli.it/libro.asp?ty=&i...07003007&mcs=0
    not only Jasmin Blanchette on german...

    But all this sample from Sudoku or raw C++ sample dont have any comparison from QT... on C++ any small function must self write to compose a class.. yesterday i look the code from Mac http://webdav.org/goliath/ (2002 last commit) .. to pick idea to my CMS webdav projekt. It have 5 file to build a Thread and Fork to get a listing from dir view http Propfind method 1200 or more line of code.. and i make the same class on 280 line qt code..

    What i want to say on QT design plattern ist not simple if you can join a QThread , a QMainwindow , a Qhttp and 3 button and play all on 2 or 1 QTreeView like my projekt http://sourceforge.net/projects/qt-webdav/ ...

    On this day i work on a big projekt .. an moore i write and going forward the difficulty was chance from the first day from projekt... I write my class on a simple main .. and if is running ok ... i put the class on a static lib path... at end i have only one or two libs and a QMainwindow , this is my actualy design plattern. I do not know other method.

  11. #30
    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: QThread exec proplem to stop...

    It's not about design patterns. It's about such simple things as using the event loop correctly In your situation I don't know why you are using threads at all...

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.