Difficulties with terminating a thread

Godot Version

4.3

Question

Hello all.

Maybe this is a newbie questions, so please be patient :slight_smile:

I am starting a thread with

var exp : Thread
exp = Thread.new()
exp.start(exp_fun)

The thread routine looks as follows:

func exp_fun():
	while true:
		await get_tree().create_timer(0.5).timeout
		print("exp_fun active")

So, effectively i output some “active” message to see if the thread is running.

Problem is: calling

exp.wait_to_finish()

does not work. The thread is still running on forever. I still see the “active” messages running.

What am i doing wrong? I try to solve this problems for hours now, and all documentations say that this should work.

Many thanks for your answers

wait_to_finish() waits until the thread is finished, but your thread is inside an endless while loop. I suggest to change the while condition to something you can control, a variable or something and then just end the while loop to end the thread

1 Like

Ending the while loop does not end the thread. Just tried it out.

You will have to show how you tried it out too or we do not know what could be going wrong.

2 Likes