<?xml version="1.0" encoding="windows-1252"?>
<node id="1014760" title="Re^3: Perl/Tk window contents disappear when obscured then revealed" created="2013-01-22 13:54:51" updated="2013-01-22 13:54:51">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;Do feel free to correct me if you still think MainLoop is appropriate here. &lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;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.  

&lt;P&gt;To achieve that you can call &lt;c&gt;$-&gt;update()&lt;/c&gt; 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 (&lt;c&gt;MainLoop;&lt;/c&gt;) 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.

&lt;p&gt;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:

&lt;ol&gt;&lt;li&gt;Just run the code and accept that the GUI will 'freeze' for the duration.

&lt;p&gt;This is what you have now. It's not nice or professional.

&lt;/li&gt;&lt;li&gt;Break the processing into small bits that take less than 1/10th of a second and arrange to call &lt;c&gt;update()&lt;/c&gt; (Or better: &lt;c&gt;DoOneEvent()&lt;/c&gt; or &lt;c&gt;DoEvents()&lt;/c&gt;) between those small chunks. 

&lt;p&gt;In practice, putting a call to &lt;c&gt;DoEvents()&lt;/c&gt; 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.

&lt;p&gt;Not all long running processing involves convenient user-code loops. Waiting for an external command is one such situation.

&lt;p&gt;And even when it does; if the loop is very tight; constantly calling out to &lt;c&gt;DoEvents()&lt;/c&gt; can substantially slow down the processing; doubling or trebling the time it takes. 

&lt;p&gt;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. 

&lt;P&gt;This can be finely tuned by only calling &lt;c&gt;DoEvent()&lt;/c&gt; 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. 

&lt;p&gt;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.

&lt;/li&gt;&lt;li&gt;Put the long running processing into a background thread or process.

&lt;p&gt;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.

&lt;p&gt;Whether a background thread or process is the best solution for your application very much depends upon what that application is.

&lt;/li&gt;&lt;/ol&gt;

&lt;p&gt;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?

&lt;p&gt;If you supply more details; we might be able to recommend a better way of tackling the problem.


&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'&lt;/div&gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;In the absence of evidence, opinion is indistinguishable from prejudice.
&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;
This can be finely tuned by only calling  for the duration.

</field>
<field name="root_node">
1014459</field>
<field name="parent_node">
1014658</field>
</data>
</node>
