# Background timer causing UI "hang" issues

**URL:** <https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130>\
**Category:** Web\
**Created:** [August 6, 2013, 7:48pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130 "2013-08-06T19:48:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Sumpter](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/ecc23a/32.png) [@Brian\_Sumpter](https://forum.xojo.com/u/Brian_Sumpter)\
**Post date:** [August 6, 2013, 7:48pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/1 "2013-08-06T19:48:57Z")

</div>

I am working on a web application, and I currently have it set up to run a timer in a thread once per minute. Unfortunately, it seems that when the timer fires the UI goes through a brief “glitch” where buttons and other controls are completely unresponsive. Is it possible I’ve set my thread up wrong, as I thought this was how to avoid UI hangs during background processes. The timer is running, so I know I at least got that part correct! 🙂

Any insight would be greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 6, 2013, 8:24pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/2 "2013-08-06T20:24:18Z")

</div>

You’ve got it backwards. Timers always run in the main (UI) thread. Thus, any code in the timer will interfere with the UI if it takes appreciable time. Have the timer run the thread, not the other way around.

---

<div class="post-metadata">

**Author:** ![Syed\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@Syed\_Hassan](https://forum.xojo.com/u/Syed_Hassan)\
**Post date:** [August 6, 2013, 9:19pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/3 "2013-08-06T21:19:54Z")

</div>

Please also see the “Threaded UI Update.xojo\_binary\_project” in Xojo 2013r2 in “\Example Projects\Desktop\Threading\” folder.

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [August 7, 2013, 2:13pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/5 "2013-08-07T14:13:24Z")

</div>

> [@25552:@Syed Hassan](#):
>
> Please also see the “Threaded UI Update.xojo\_binary\_project” in Xojo 2013r2 in “\Example Projects\Desktop\Threading\” folder.

These only apply to desktop apps, not web apps.

---

<div class="post-metadata">

**Author:** ![Brian\_Sumpter](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/ecc23a/32.png) [@Brian\_Sumpter](https://forum.xojo.com/u/Brian_Sumpter)\
**Post date:** [August 7, 2013, 6:04pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/6 "2013-08-07T18:04:41Z")

</div>

OK, I’ve flipped it around so that the timer is now calling the thread, and all code that is being executed is now within that thread.

Unfortunately, I’m still seeing the UI hang issues when the timer spawns the thread. Here’s my process as it stands now:

1. App.Open calls the “OpenTimer()” method.
2. The “OpenTimer()” method contains the following code:

` mytimer = new classtimer
  mytimer.period = 30000
  mytimer.mode = 2`  
3) There is a class called “ClassTimer” which has “Timer” as its super.  
4) ClassTimer.Action contains the following code:

` Dim timerThread as New ClassThread
  timerThread.Run`  
5) There is a class called ClassThread which has “Thread” as its super.  
6) In ClassThread.Run there is the following code:

[code] Dim rs As RecordSet  
Dim query As String = “SELECT db\_hostname, db\_port, db\_username, db\_password FROM configuration”  
rs = system\_db.SQLSelect(query)

If Not mysql\_connect\_from\_config() Then  
Else

If Main.monitorActive Then  
LoadMySQLVars()  
sysinfo\_Timed()  
LoadRules()  
End If

mysql\_db\_close()  
End If[/code]

I _think_ I’m doing this correctly, and everything does work properly with the exception of the UI hang when the timer is running.

Any insight would be greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 7, 2013, 6:56pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/7 "2013-08-07T18:56:53Z")

</div>

You must ensure that your thread yields time to the UI. You might try lowering the priority of the thread. Or put an explicit app.YieldToNextThread in your loops (not every iteration - use ticks or microseconds to make it “sane”). And check for any database or system call that might hang. A long-executing sql call will hang your app until it completes, thread or no thread.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:47pm UTC](https://forum.xojo.com/t/background-timer-causing-ui-hang-issues/10130/8 "2020-10-29T16:47:40Z")

</div>


