in reply to Re^2: Perl/Tk window contents disappear when obscured then revealed
in thread Perl/Tk window contents disappear when obscured then revealed
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.
This can be finely tuned by only calling for the duration.
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.