Refresh screen during a loop ?

Hi,

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, …) ?

Thank you

call the method App.DoEvent(1) inside the loop.

You should consult the docs. It is not advisable to use App.DoEvents in a desktop application. See: Application.DoEvents.

And, excepted if you do not really care, refreshing the inteface have a coast: it slow down, really slow down the loop speed…

But, it may be fun to see, say, a new line appearing in a ListBox (or disappears if it is a download manager for example)…

Use the Task class (in the examples) to update the textfields.

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.

The correct answer is the one given by Michel. Use a thread

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.

sorry I still use RS2012 where this works fine … will have to update my sources when I switch to xojo … :wink:

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.

It does work in Xojo as well. But it can have adverse effects, including undesirable reentry and difficult to find bugs. See this lively thread : https://forum.xojo.com/10498-making-the-case-to-remove-app-doevents

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.