I’ve a big computation done in a big loop that takes a couples of seconds (70 to 80) to end. In this loop I want to tell the user the progress of the computation. So I update some textfields. But nothing refresh during the loop. The windows content is only fully refreshed at the end of the loop. Is there a command able to give some time to the app to refresh correctly the window during the loop and to give some time to the other applications that run in background (safari, ) ?
A quick experiment shows that timers do not fire when a tight loop is used on the main thread. Indeed App.Doevent allows update of the UI. But since doevent is unsafe, rather not do it.
The simplest way I found is to place the calculation in the Run event of a thread, update a window property in there, and use a timer to fetch that property and update the Textfield at regular intervals.
It uses the same principle as the task class, but is much simple to do.
In a button :
Sub Action()
Thread1.run
End Sub
In the Thread :
Sub Run()
for i as integer = 1 to 65535
for y as integer = 1 to 2000
next
toto = i // Toto is a window property that will be used to update the textfield
next
End Sub
In a 200 ms multiple timer :
Sub Action()
textfield1.text = str(toto)
textfield1.Invalidate
End Sub
Of course I have no pretention about the mathematics involved in my quick test. They are just there to make it display a value.
In one of my apps a lot of file reading and processing has to take place upon selecting the data file to process. On the window I have a label field that shows the process; however, it is only updated every 100th execution of the loop. I simply Refresh the label field. The code looks like the following, minus declares but it should give the idea.
// now read in the trace headers and their corresponding traces
while bs.Position < bs.Length
num = num + 1
if num mod 100 = 0 then // keep the progress line above the listbox active
lblNowShowing.text = "Processing trace " + Format(num, "#")
lblNowShowing.Refresh
end if
readTraceHeader // reads the 240 bytes for next trace header and appends to th array
readTraceData // reads th(n).NofSamples sample values for next trace appends to trace array
wend
This does not slow down the execution noticeably but does allow the progress to be displayed.
[quote=178083:@Harrie Westphal]In one of my apps a lot of file reading and processing has to take place upon selecting the data file to process. On the window I have a label field that shows the process; however, it is only updated every 100th execution of the loop. I simply Refresh the label field. The code looks like the following, minus declares but it should give the idea.
// now read in the trace headers and their corresponding traces
while bs.Position < bs.Length
num = num + 1
if num mod 100 = 0 then // keep the progress line above the listbox active
lblNowShowing.text = "Processing trace " + Format(num, "#")
lblNowShowing.Refresh
end if
readTraceHeader // reads the 240 bytes for next trace header and appends to th array
readTraceData // reads th(n).NofSamples sample values for next trace appends to trace array
wend
This does not slow down the execution noticeably but does allow the progress to be displayed.[/quote]
I tried that but a tight calculation loop prevents the refresh to take place.
Notice those two calls to methods at the bottom of the loop. That gives it a spot to do the update as each section of the file is read in and processed in those two methods. Header then data then another header and then that headers data and so on. It is data gathered from explosions performed when testing for oil deposits. So, the loops are tight but there are enough breaks to allow the screen update to refresh rapidly. If it all has to be done in a single loop with no method calls then you are probably SOL. Here I had a perfect opportunity to not put all the code in one tight loop.
I may elect to cross a busy highway by closing my eyes and making a dash for it. Upon safely reaching the other side, I may well conclude that this method “works fine” and choose to disregard the warnings of law enforcement officials and my cautious friends that this is not a safe way to cross the highway.