# Re: hang on leopard

**URL:** <https://www.ruby-forum.com/t/re-hang-on-leopard/129790>\
**Category:** wxRuby\
**Created:** [February 21, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/re-hang-on-leopard/129790 "2008-02-21T16:10:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhang\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/cdc98d/32.png) [@Zhang\_P](https://www.ruby-forum.com/u/Zhang_P)\
**Post date:** [February 21, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/re-hang-on-leopard/129790/1 "2008-02-21T16:10:04Z")

</div>

Hi Alex,

Thank you so much for taking time to give me the guidance!

I’ll try your suggestion and remove the threads and see what happens …

Thanks again!

----- Original Message ----  
From: Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: General discussion of wxRuby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, February 20, 2008 5:35:41 PM  
Subject: Re: [wxruby-users] hang on leopard

Zhang  
Peng  
wrote:

> 

If  
it  
is  
a  
thread  
issue,  
I  
wonder  
why  
I  
couldn’t  
duplicate  
it  
on  
XP?

> 

Since  
I  
can’t  
duplicate  
it  
on  
XP,  
I  
was  
thinking  
that  
it  
is  
not  
just  
a

> 

pure  
thread  
issue,  
but  
Wx  
for  
OSX  
+  
ruby  
thread  
combination  
is  
the

> 

culprit.  
Because  
wxRuby  
/  
wxWidgets  
uses  
different  
native  
platform  
functions  
internally,  
there’s  
no  
guarantee  
that  
if  
you’re  
doing  
something  
wrong,  
it  
will  
fail  
in  
the  
same  
way,  
or  
at  
all,  
on  
different  
platforms.  
But  
it  
should  
always  
work  
if  
done  
right  
…

> 

Thanks  
for  
offering  
help  
on  
design  
choice.  
I  
appreciate  
it  
!

> 

I  
don’t  
actually  
have  
a  
lot  
of  
globals  
and  
threads  
…  
only  
2  
globals

> 

and  
3  
threads.  
Let  
me  
try  
to  
explain:

> 

I  
have  
one  
dialog  
box  
that  
shows  
a  
number  
flashing  
on  
the  
screen  
one

> 

after  
another,  
and  
another  
dialog  
box  
that  
shows  
the  
time  
and  
a  
input

> 

text  
field  
to  
collect  
the  
answer  
the  
user  
types  
in  
for  
adding  
up  
all

> 

those  
numbers  
from  
the  
other  
dialog.  
OK.  
First  
of  
all,  
I  
really  
don’t  
think  
you  
need  
to  
use  
threads  
or  
globals  
at  
all  
to  
achieve  
this.  
wxRuby  
like  
other  
GUI  
packages  
is  
essentially  
event  
driven:  
updates  
to  
the  
GUI  
should  
be  
driven  
by  
events.  
This  
includes  
user  
interaction  
(clicking  
a  
button)  
or  
timed  
events  
(a  
timer  
ticks).

> 

So,  
I  
have  
2  
global  
variables:  
one  
for  
each  
instance  
of  
the  
dialog

> 

box.  
I  
use  
global  
because  
I  
do  
not  
want  
to  
destroy  
and  
create  
a  
new

> 

instance  
of  
the  
dialog  
box  
every  
time  
a  
user  
want  
to  
do  
the  
exercise.  
Fair  
enough.  
You  
could  
make  
them  
instance  
variables  
of  
your  
Wx::App  
class,  
create  
them  
when  
the  
app  
starts,  
and  
provide  
an  
accessor  
to  
them.  
Then  
you  
could  
access  
them  
anywhere  
in  
your  
code  
with

Wx::THE\_APP.number\_dialog  
etc.

> 

I  
have  
3  
threads:  
one  
to  
show  
the  
flashing  
number,  
one  
to  
collect

> 

answer,  
and  
one  
for  
showing  
the  
time.  
I  
use  
WxTimer.every()  
for  
the

> 

timer  
per  
your  
suggestion.  
I  
use  
ruby  
thread  
for  
the  
other  
2  
as  
you

> 

see  
in  
the  
code  
I  
posted  
yesterday.  
I  
was  
suggesting  
you  
use  
Wx::Timer  
in  
place  
of  
threads.  
A  
timer  
to  
update  
the  
status  
bar  
with  
the  
current  
time  
doesn’t  
need  
to  
be  
much  
more  
complicated  
than:

# Wx::Timer.every(1000) { self.status\_text

Time.now.to\_s  
}

No  
threads  
needed…

> 

So,  
when  
user  
hit  
the  
button,  
both  
ruby  
threads  
will  
start/resume

> 

their  
tasks:  
one  
to  
show  
the  
number  
and  
one  
to  
collect  
the  
answer,  
and

> 

WxTimer  
shows  
the  
time.  
You  
can  
catch  
when  
the  
button  
is  
clicked  
with  
evt\_button,  
and  
then  
just  
call  
a  
method  
on  
the  
other  
dialog  
to  
tell  
it  
to  
stop  
displaying  
numbers.  
Again,  
I’m  
sure  
you  
don’t  
need  
threads  
here.

Finally,  
you  
can  
catch  
when  
characters  
are  
entered  
in  
a  
TextCtrl  
using  
evt\_text.  
Or,  
if  
you  
want  
to  
monitor  
the  
TextCtrl’s  
contents  
continually,  
again,  
just  
use  
a  
Timer  
and  
call  
text\_ctrl.value.

If  
you  
need  
to  
stop  
and  
start  
Timed  
actions,  
you  
can  
capture  
the  
return  
value  
of  
Wx::Timer.every  
and  
call  
stop  
on  
it:

# @number\_timer

Wx::Timer.every(2500)  
do

display\_number

# 

call  
code  
to  
display  
a  
new  
number  
on  
the  
screen  
end

@number\_timer.stop

# 

Stop  
the  
timer  
ticking  
@number\_timer.start

# 

Restart  
it  
later

hth  
alex

* * *

wxruby-users  
mailing  
list  
removed\_email\_address@domain.invalid  
[http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

```
  ____________________________________________________________________________________

```

Be a better friend, newshound, and  
know-it-all with Yahoo! Mobile. Try it now.  
[http://mobile.yahoo.com/;\_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ](http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)
