Tk/thread problem

Hi all,

I have a Tk/GUI containing two buttons: auto and stop.
When I press the auto_button which contains a new thread, the script
will loop through an array and show up the elements in the array
automatically.
When I press the stop_button which also contains a new thread, the loop
will stop.

If I don’t add text_to_speech method to auto_button and ask the script
to read the text/element for me, the script behaves as expected.

But if I add text_to_speech method to the auto_button, then press
auto_button, I find the script freezes most of the time, during which it
is difficult to response to press stop_button.

I wonder anyone can help me solve this problem. Thanks in advance.

#########part of my script#############
#$z is the array being looped through

$buttons[0].command proc{ #this is the auto_button
$button_0_thread=Thread.new{

(0…100).each do |i|
$question_text.delete “1.0”,“end”
$answer_text.delete “1.0”,“end”
$page_text.delete “1.0”,“end”

$question_text.insert "end","#{$z[i][0]}"
sleep 0.1
#text_to_speech("#{$z[i][0]}")
sleep 0.1



$answer_text.insert "end","#{$z[i][1]}"
#text_to_speech("#{$z[i][1]}")
sleep 0.1

end
}#thread

}#$buttons[0]

$buttons[1].command proc{#this is stop_button
Thread.kill $button_0_thread
}

Try to stop each thread in the first line of the thread. Then join both
together, and see what happens. For example:

a = Thread.new {
Thread.stop
// stuff
// instead of sleep you can try to use pass()
Thread.pass
// stuff
}

b = Thread.new {
Thread.stop
// stuff
// instead of sleep you can try to use pass()
Thread.pass
// stuff
}

[a, b].each {|t| t.join}

Threads in Ruby might not work as you think. The threads does not run in
parallel. Perhaps you have to learn/read a bit more. Saludos.

The results cannot be predicted.