Display counter in loop

Hi everyone
I have a loop (for i as integer)
inside the loop it trigger a applescript whos open indesign doc… print… close and return a report as text
it take about 3 sec for each
I want to display the counter, to know what is done
But it display only when the loop is finish
Why it can’t display between each pass

for i = 0 to LBDropList.ListCount-1 myReturnTxt=AppScriptXportPDF3(myXportPreset,myRegExNameRet,myXportPath,myXportparent) LabelKountX.Text = str(i) Next
Pretty easy question… but if it’s a CPU or conflict matter… it’s out of my knowledge

For one thing the loop doesn’t yield time to the rest of your app - that’s why long running loops are often done in threads, to keep the interface responsive. You can force an interface refresh by using LabelKountX.Refresh but that might lead to flicker.

A better way to do this is

add an integer variable to your window called LoopCount
add a timer to your window called LoopTimer, Mode = off, Period 250 ms

in your loop have:

LoopCounter = 0 // reset the counter to 0 LoopTimer.enabled = True LoopTimer.Mode = Timer.ModeMultiple // also starts the timer for i = 0 to LBDropList.ListCount-1 myReturnTxt=AppScriptXportPDF3(myXportPreset,myRegExNameRet,myXportPath,myXportparent) LoopCounter = LoopCounter + 1 Next LoopTimer.Mode = Timer.ModeOff LoopTimer.enabled = False

in LoopTimer’s action event

LabelKountX.Text = str( LoopCounter ) LabelKountX.invalidate

You could also make a subclass of Timer and add your count variable there.

Thanks Markus
I understand what is all about
But it doesn’t work… even when the loop finished the label is 0 ( but the variable is 4 )
the only way that i succeeded to display is when i remove the

LoopTimer.Mode = Timer.ModeOff LoopTimer.enabled = False
But it only appear at the end only

right after
LabelKountX.Text = str(i)

Thanks Norman
enough slow loop, to not flick
Thanks Markus… i’v at least learned the timer mechanism

Put your loop into a thread, then it will work and leave your app responsive.