# Update UI while looping. Yes, Again

**URL:** <https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072>\
**Category:** Windows\
**Created:** [November 29, 2021, 4:30pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072 "2021-11-29T16:30:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 4:30pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/1 "2021-11-29T16:30:48Z")

</div>

So, big loop.  
Lots to do.  
Desktop.  
Mostly Windows…

How the heck do you get the screen to update and show a progress?  
Either Windows starts complaining that the app is not responding and wants to kill it, or we try to use DoEvents which is a Bad Thing (and rarely solves the issue)

I have a nice graphic to show the progress - works fine on Mac, but doesnt update in Windows and after 18 years of using Xojo Im sick of this situation.  
Surely there must be a way to do this ?  
Games update the screen a lot more times per second than I need to.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [November 29, 2021, 4:47pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/2 "2021-11-29T16:47:57Z")

</div>

a method call block the ui until your method exists.  
therefore the unhandily thread class was created.  
without this thread  
instead of using for i = 1 to 1000  
process this list in parts and let the app breathe.  
as example a timer could fetch next small task from a list and process it.

in games the ui and game/physic “loop” are usually separate.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 4:50pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/3 "2021-11-29T16:50:11Z")

</div>

> [@MarkusR](#):
>
> example a timer could fetch next small task from a list and process it.

Not the kind of work that can be passed to many little timers.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [November 29, 2021, 4:52pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/4 "2021-11-29T16:52:26Z")

</div>

> [@Jeff\_Tullin](#):
>
> big loop

have you an example what your “big loop” do?

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [November 29, 2021, 4:53pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/5 "2021-11-29T16:53:40Z")

</div>

> [@Jeff\_Tullin](#):
>
> Not the kind of work that can be passed to many little timers.

My 2 cheater methods are:

- CallDelegateOnMainThreadMBS AddressOf
- xxxThreadSafeMBS

---

<div class="post-metadata">

**Author:** ![Anthony\_Dellos](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_dellos/32/208_2.png) [@Anthony\_Dellos](https://forum.xojo.com/u/Anthony_Dellos)\
**Post date:** [November 29, 2021, 4:54pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/6 "2021-11-29T16:54:15Z")

</div>

Have you tried using [https://documentation.xojo.com/api/language/thread.html#thread-adduserinterfaceupdate](https://documentation.xojo.com/api/language/thread.html#thread-adduserinterfaceupdate) and set your thread’s [priority](https://documentation.xojo.com/api/language/thread.html#thread-priority) to something like “5”? (“normal priority”)

---

<div class="post-metadata">

**Author:** ![anon20074439](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@anon20074439](https://forum.xojo.com/u/anon20074439)\
**Post date:** [November 29, 2021, 4:55pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/7 "2021-11-29T16:55:05Z")

</div>

1. Are you using a thread for your loop
2. Are you using Control.Refresh to update your progress bar and have moved the control to the new Desktop types?

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 4:55pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/8 "2021-11-29T16:55:07Z")

</div>

some areas,

```auto
for x as integer = 0 to 9999
for y as integer = 0 to 9999
//many interesting calcs
next
next

```

in another, the unpacking of 2000 small files as part of the initial installation, checking for presence of folders and dates of existing files

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 4:57pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/9 "2021-11-29T16:57:41Z")

</div>

> [@anon20074439](#):
>
> - Are you using a thread for your loop
> - Are you using Control.Refresh to update your progress bar and have moved the control to the new Desktop types?

No.  
For **some** of the things I need to do, I need the app to wait for the process to complete. I cant have things happening while a thread does its stuff.

And I don’t want to go anywhere near those pointless new Desktop types.  
Do they have some new functionality I missed?

---

<div class="post-metadata">

**Author:** ![anon20074439](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@anon20074439](https://forum.xojo.com/u/anon20074439)\
**Post date:** [November 29, 2021, 4:59pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/10 "2021-11-29T16:59:15Z")

</div>

> [@Jeff\_Tullin](#):
>
> Do they have some new functionality I missed?

More like a change in functionality so I was just working out where you are before I offer some help, trying to figure out the best way to get you where you want to be as there may be a workaround without getting into a rewrite.

How are you telling your progress bar to update?  
Is it on a canvas/control so Invalidate or Refresh?  
Or are you using a progress bar control?

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 5:08pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/11 "2021-11-29T17:08:01Z")

</div>

> [@anon20074439](#):
>
> How are you telling your progress bar to update?  
> Is it on a canvas/control so Invalidate or Refresh?  
> Or are you using a progress bar control?

If I hone in on the specific instance I am trying to resolve now:

I have a tight loop for those files.  
I open a window that has a progress bar and a canvas on it.  
When the number of files MOD some number = 0, I issue

```auto
progwin.thecanvas.invalidate (or refresh)
and
progwin.theprogressbar.invalidate (or refresh)

and I have tried app.doevents in there too.

```

Ive also moved the updates to the progress window:  
I update a property in the loop, and have a timer on the progress window which fires every so often and tries to update the screen.

Also no effect.

Its my understanding that using a thread, I cannot affect the screen, so it seems Im stuck

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [November 29, 2021, 5:08pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/12 "2021-11-29T17:08:41Z")

</div>

in case of your loops  
a single call would be “one interesting calc x,y”  
after it is processed do the next from a prepared list.  
you could also process for a second a bunch and then let the app response  
a timer could trigger the next process.  
Thread or Worker could be useful for you too.

using invalidate to much could result in nothing because windows ignore it.  
and the method where you call it must end that the ui update comes.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [November 29, 2021, 5:08pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/13 "2021-11-29T17:08:50Z")

</div>

Here’s how to do it:

- move your computations to a Thread
- before starting the Thread, open a Modal Dialog window with a progress bar
- the thread can update the progress bar (using [https://documentation.xojo.com/api/language/thread.html#thread-adduserinterfaceupdate](https://documentation.xojo.com/api/language/thread.html#thread-adduserinterfaceupdate) or a Timer that you create)
- the modal dialog prevents any other user action.
- once the thread is done, close the modal window

I use this pattern all the time in my apps, and it works great.

---

<div class="post-metadata">

**Author:** ![Ivan\_Tellez](https://forum.xojo.com/letter_avatar_proxy/v4/letter/i/a9a28c/32.png) [@Ivan\_Tellez](https://forum.xojo.com/u/Ivan_Tellez)\
**Post date:** [November 29, 2021, 5:13pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/14 "2021-11-29T17:13:16Z")

</div>

> [@Jeff\_Tullin](#):
>
> For **some** of the things I need to do, I need the app to wait for the process to complete. I cant have things happening while a thread does its stuff.

> [@Jeff\_Tullin](#):
>
> I need the app to wait for the process to complete. I cant have things happening while a thread does its stuff.

Wll, @Mike_D won lol, what he said is the way to do in xojo

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 29, 2021, 5:17pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/15 "2021-11-29T17:17:45Z")

</div>

Another option is my [Looper](https://github.com/ktekinay/Xojo-Looper) control. Drag it to the window and implement the events like HandleIteration, MoveNext, and Paused. It emulates what a Thread will do for you, but on the Main Thread and with finer control for updates.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 5:19pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/16 "2021-11-29T17:19:44Z")

</div>

Thanks for the ideas. Something to play with.  
Sadly, I haven’t been able to actually use any Windows Xojo version after 2015 , so that Thread.adduserInterfaceupdate isnt available to me in this _particular_ project.  
New ones, yes.  
But maybe the modal window that uses a timer to look at common properties which are affected by a thread…

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [November 29, 2021, 5:21pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/17 "2021-11-29T17:21:30Z")

</div>

> [@Jeff\_Tullin](#):
>
> ```auto
> for x as integer = 0 to 9999
> for y as integer = 0 to 9999
> // many interesting calcs
> next
> next
> 
> ```

In the case of tight loops, I find it really helps to add  
`#pragma DisableBackgroundTasks`

to your methods, and then occasional calls to  
`App.SleepCurrentThread(1)`

in the outer loop, e.g.  
`if x mod 1000 = 1 then App.SleepCurrentThread(1)`  
and play with the values here until you get a good combination of speed vs. UI responsiveness.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 29, 2021, 5:23pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/18 "2021-11-29T17:23:58Z")

</div>

Thanks. I’ll take a look.

---

<div class="post-metadata">

**Author:** ![Bob\_Keeney3](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@Bob\_Keeney3](https://forum.xojo.com/u/Bob_Keeney3)\
**Post date:** [November 29, 2021, 6:26pm UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/19 "2021-11-29T18:26:18Z")

</div>

> [@Jeff\_Tullin](#):
>
> Sadly, I haven’t been able to actually use any Windows Xojo version after 2015 , so that Thread.adduserInterfaceupdate isnt available to me in this _particular_ project.

You can implement that same thing on a Thread class from that era. Subclass the thread, add a timer and when you need the UI to update you start the timer. The timer.Action _always_ fires on the main thread. I queue a dictionary and pass whatever I have accumulated in the action event (again which fires on the main thread) and let the UI update then. There are various implementations of this floating around and if you need I can dig out my old BKSThread class that did this.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [November 30, 2021, 9:38am UTC](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072/20 "2021-11-30T09:38:08Z")

</div>

There is no need for plugins (if one would not require them).

Cheating is pretty simple have an example here:  
[https://dutchhype.com/static/xojo/set\_values\_thread\_safe.xojo\_binary\_project](https://dutchhype.com/static/xojo/set_values_thread_safe.xojo_binary_project)

This uses Timer.CallLater(0, delegate, value) to re-assign the given on the main thread even when called from any thread. This can be done with any control value.

It simplifies UI updating and makes stuff more readable (from the thread’s code).

[Next page](https://forum.xojo.com/t/update-ui-while-looping-yes-again/67072.md?page=2)
