PDA

View Full Version : 'Processing' message


arnaldo
06-25-2007, 06:15 AM
Hi everybody,
I have a method which takes some seconds to execute, and I want to let the user know when it is running. So I wrote the following code:

<canvas>
<method name="slowProcess">
<![CDATA[
var c = 1.000001;
for(var i=0;i<1000;i++)
for(var j=0;j<400;j++)
c = Math.pow(c,c);
]]>
</method>
<simplelayout axis="x"/>
<button>Process
<handler name="onclick">
txtProcessing.setVisible(true);
canvas.slowProcess();
txtDone.setVisible(true);
</handler>
</button>

<text id="txtProcessing" visible="false">Processing...</text>
<text id="txtDone" visible="false">Done</text>
</canvas>

However, the "Processing..." text only appears when the process has finished.
How can I make it work?

senshi
06-25-2007, 12:14 PM
As you're working in a single-threaded process (regardless of SWF-runtime or DHTML-runtime), you must also give the underlying system (flash resp. browser) enough time for updating/rendering your GUI.
(Note: In this updated example, the Debug-Output has got the same issues as you've reported, but the actual User-Message will be shown properly.)


<canvas debug="true" >

<event name="onprocessfinished" />

<method name="slowProcess">
<![CDATA[
var start = (new Date()).getTime();
Debug.write( "starting process..." );
var c = 1.000001;
for(var i=0;i<1000;i++)
for(var j=0;j<400;j++)
c = Math.pow(c,c);
Debug.write( "process has terminated. (time elapsed: " + ((new Date()).getTime()-start) + ")" );
canvas.onprocessfinished.sendEvent();
]]>
</method>

<view>
<simplelayout axis="x"/>
<button text="Process" >
<attribute name="del" value="null" />
<handler name="onclick">
txtProcessing.setVisible( true );
if( !this.del ){
this.del = new LzDelegate( canvas, "slowProcess" );
}
LzIdle.callOnIdle( this.del );
</handler>
<handler name="onprocessfinished" reference="canvas" >
txtDone.setVisible( true );
</handler>
</button>

<text id="txtProcessing" visible="false">Processing...</text>
<text id="txtDone" visible="false">Done</text>
</view>

</canvas>

arnaldo
06-26-2007, 04:15 AM
Thank you very much!

ptw
06-26-2007, 12:51 PM
Be careful here. The idle loop in both DHTML and SWF will silently terminate any loop that iterates too many times or takes too long to complete. If you want to run a long-running process in the idle loop so that it does not preclude UI updates, you need to limit it. (See for example Debug.traceStep in WEB-INF/lps/lfc/debugger/platform/swf/LzMemory.as) You need to make your long-running background process into a co-routine that limits the processing time and reschedules itself again in the idle loop if there is more work to do.

This is most likely why you are not seeing your debug output.