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!
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.
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:
App.Open calls the “OpenTimer()” method.
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.
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.