Do feel free to correct me if you still think MainLoop is appropriate here.
The nature of GUI's is that whenever a window (or part thereof) is uncovered; or has its size changed, you have to re-draw the contents of that window.
To achieve that you can call $->update() within long running loops; but that will often mean that updates are forced each time around the loop when they are not needed -- ie. you chew CPU for no reason -- or you can enter the event loop (MainLoop;) which will monitor the event queue and call update() only when it is required; along with servicing any and all other events as they occur.
If you have a piece of code to run that is going to take more than say 1/10th of a second, then you have three choices:
- Just run the code and accept that the GUI will 'freeze' for the duration.
This is what you have now. It's not nice or professional.
- Break the processing into small bits that take less than 1/10th of a second and arrange to call update() (Or better: DoOneEvent() or DoEvents()) between those small chunks.
In practice, putting a call to DoEvents() into a convenient place in your processing loop works well enough provided that you have a loop and its frequency is neither too fast nor too slow.
Not all long running processing involves convenient user-code loops. Waiting for an external command is one such situation.
And even when it does; if the loop is very tight; constantly calling out to DoEvents() can substantially slow down the processing; doubling or trebling the time it takes.
Conversely, if the loop itself takes long than 1/10th of a second to iterate, the GUI can become sluggish to respond to user input.
This can be finely tuned by only calling DoEvent() every 5th, or 50th or 500th iteration for the fast loop; or calling it at multiple places for the slow loop; but it requires trial and error to strike the optimal balance between GUI responsiveness and processing throughput.
But the big problem is that when you've achieve that fine balance on your development machine and move it to a different machine; the balance is lost because that machine has a faster or slower CPU; memory; disk; or a higher background workload; or any of a dozen other differences that screw up your hard one balancing act.
- Put the long running processing into a background thread or process.
This is best of all, because the OS scheduler takes care of balancing the needs of the GUI responsiveness with the CPU requirements of the processing.
Whether a background thread or process is the best solution for your application very much depends upon what that application is.
What is this program that you are running in the background? Does it produce output that you need in your GUI application? Does it produce a file that you then need to read?
If you supply more details; we might be able to recommend a better way of tackling the problem.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
This can be finely tuned by only calling for the duration.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||