Multithreading help... If thats what i need?
-
I need to run a file compressor from GUI but i also need to show the user that something is happening while the file is compressing.I have a dial that is supposed to start spinning when the compression starts and stop spinning when the compression is complete.The method to spin the dial is on a timer ,i made the timer start ,then called the compression method ,the problem is ,once the compression starts the dial stops spinning.After the compression the dial spins again ,i need the dial to spin DURING compression,please help.
-
In your case, it should be enough to call
@
qApp->processEvents();
@
occasionally during compression. If you "own" the compression code, you can insert it somewhere in an inner loop -- but make sure it's not called every single iteration, otherwise performance goes down too much.If you've got a counter somewhere, that runs up the number of bytes or whatever, and loop index is i, you could for example do this:
@
if (i % 1024 == 0) {
qApp->processEvents();
}
@
This would only execute every 1024th time.If you compress the file using a single "external" call however, like qCompress, I'd suggest you use QtConcurrent. Look at http://doc.qt.nokia.com/latest/qtconcurrentrun.html . I'd suggest you do something like:
@
QFuture<QByteArray> future = QtConcurrent::run(qCompress, myByteArrayDatat);
future.waitForFinished();
QByteArray uncompressed = future.result();
@
Then your event loop keeps running during compression... -
Several of these techniques are explained in my document here http://developer.qt.nokia.com/wiki/Threads_Events_QObjects .
Btw:
[quote author="LinusA" date="1312149978"]I'd suggest you do something like:
@
QFuture<QByteArray> future = QtConcurrent::run(qCompress, myByteArrayDatat);
future.waitForFinished();
QByteArray uncompressed = future.result();
@
Then your event loop keeps running during compression...
[/quote]Read that again. waitFor - event loop keeps running. Do you see it?
-
[quote author="peppe" date="1312153167"]
Read that again. waitFor - event loop keeps running. Do you see it?[/quote]
Oups, silly mistake, thanks for pointing out. Of course this would NOT run the event loop. Better use a "QFutureWatcher":http://doc.qt.nokia.com/latest/qfuturewatcher.html . Great wiki page, peppe!