Thread acting as a timer issue

Hi folks.

I have run into an issue that is making me frustrated.

I’m running Xojo 2025R2 on mac Sonoma.

I have a thread that will update on a window a label, so people can see the program is running, even if it isn’t showing anything.

here is the code for threadForRunningTime.Run

static runTimerValue As Integer

While app.runningTime = TRUE

runTimerValue = runTimerValue + 1

Me.AddUserInterfaceUpdate("timeCounter":runTimerValue)

Thread.SleepCurrent(1000)

Wend

And in my threadForRunningTime.UserInterfaceUpdate:

Var seconds As Integer

If data.LastIndex >= 0 Then

Var arg As Dictionary = data(data.LastIndex)

If arg.HasKey("timeCounter") Then
seconds = arg.Value("timeCounter").IntegerValue
labelProgramRunTime.Text = seconds.ToString
End If

End If

The issue I have is that instead of getting 1..2..3.. it screams up to tens of thousands. And just stops.

Or locks up my program and xojo.

If I remove the Me.AddUserInterface… it seems to act normally according to my log file.

7:12:19 PM : There are 45 total rows
7:12:21 PM : ******** MATCH FOUND ********
: I = 4 J = 6
: ******** MATCH FOUND ********
: I = 4 J = 18
: ******** MATCH FOUND ********
: I = 4 J = 20
7:12:22 PM : ******** MATCH FOUND ********
: I = 6 J = 18
: ******** MATCH FOUND ********
: I = 6 J = 20
7:12:23 PM : ******** MATCH FOUND ********
: I = 9 J = 16
: ******** MATCH FOUND ********
: I = 9 J = 19
: ******** MATCH FOUND ********
: I = 9 J = 23
7:12:24 PM : ******** MATCH FOUND ********
: I = 11 J = 12
7:12:25 PM : ******** MATCH FOUND ********
: I = 13 J = 37
: ******** MATCH FOUND ********
: I = 14 J = 22
7:12:26 PM : ******** MATCH FOUND ********
: I = 15 J = 43
: ******** MATCH FOUND ********
: I = 16 J = 19
: ******** MATCH FOUND ********
: I = 16 J = 23
: ******** MATCH FOUND ********
: I = 17 J = 21
: ******** MATCH FOUND ********
: I = 18 J = 20
7:12:27 PM : ******** MATCH FOUND ********
: I = 19 J = 23
7:12:28 PM : ******** MATCH FOUND ********
: I = 24 J = 31
7:12:29 PM : ******** MATCH FOUND ********
: I = 26 J = 27
: ******** MATCH FOUND ********
: I = 28 J = 33
7:12:32 PM : Done and Dusted
: ABOUT TO STOP RUNNING TIME
: RUNNING TIME IS NOW: False
7:1

Any ideas?

Regards

If the thread isn’t doing anything other than acting as a timer, just use a timer.

Hi Clifford.

Thank you for the response.

The reason I am using a thread as a timer is I have the program doing processor heavy work. If I try to do it with a timer, my program doesn’t update until after the work has been completed.

Regards

AddUserInterfaceUpdate uses a Timer internally.

Have you tried using Thread.YieldToNext() instead of Thead.SleepCurrent(1000)?

Are you using preemptive threads?

So is the “processor heavy work” going on in the main thread? This sounds backwards to me.

Thanks Andrew.

I’m getting so many good hints.

Thank you.

I will try and advise.

Regards

Yes, Eric.

Thanks for the response.

Regards