No, not OK.
isFinished always returns false when called within thread ( especially from run ).
:)
Printable View
this run .... && having time out if loop....
is this better ....?
Code:
{ Q_OBJECT public: void run(); signals: void TimeEnd(); private: QUrl urltogrep; QString ned; HTTP_propfind *jobb; bool waiting; int decimalsec; public slots: void grepdata(); }; void DavListDir::grepdata() { ned = jobb->GetDAVData(); if (ned.size() > 0) { emit DataXml(ned); waiting = false; quit(); } } { decimalsec = 0; waiting = true; jobb = new HTTP_propfind(urltogrep); /* QHttp first setup header .... */ connect(jobb, SIGNAL(done(bool)), this, SLOT(grepdata())); } void DavListDir::run() { if (!urltogrep.isValid()) { quit(); return; } jobb->start(); /* QHttp start request */ exec(); while( waiting ) { decimalsec++; msleep(100); /* time out remote server! 2.5 sec .*/ if (decimalsec > 25) { waiting = false; ned ="time_out"; emit DataXml(ned); quit(); } } }
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.
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.Code:
while(wainting) msleep( 200 );
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.
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.
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? ...
Code:
{ Q_OBJECT public: void run(); signals: void TimeEnd(); private: QUrl urltogrep; QString ned; HTTP_propfind *jobb; bool waiting; int decimalsec; public slots: void grepdata(); void httperror(); }; void DavListDir::httperror() { emit DataXml(jobb->errorString()); jobb->abort(); exit(); } void DavListDir::grepdata() { ned = jobb->GetDAVData(); if (ned.size() > 0) { emit DataXml(ned); waiting = false; exit(); return; } } { decimalsec = 0; waiting = true; jobb = new HTTP_propfind(urltogrep); /* QHttp first setup header .... */ connect(jobb, SIGNAL(done(bool)), this, SLOT(grepdata())); } void DavListDir::run() { if (!urltogrep.isValid()) { waiting = false; ned ="url_invalid"; emit DataXml(ned); exit(); return; } jobb->start(); /* QHttp start request */ exec(); }
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.
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.
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.
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...