# Ruby-Thread and Gtk

**URL:** <https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520>\
**Category:** Ruby-Gnome 2\
**Created:** [November 10, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520 "2006-11-10T17:06:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 10, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/1 "2006-11-10T17:06:48Z")

</div>

Hi to all,

I did all my best and try to find out a way how to use Ruby with Gtk and  
Threads.  
I need an example, or a better explaining about how this things works.  
I found something like that  
“In Ruby/GTK, you should use Ruby-Thread instead of  
Gdk::Threads[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AThreads](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AThreads).  
It’s a well known issue what ruby thread doesn’t work well with  
Native-Thread. Almost of all case, Ruby-Thread works well and you don’t  
need  
call these functions in Ruby.” in here:  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AThreads](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3AThreads)  
but, it still doesn’t work for me.

Here is my situation:

require ‘mysql’  
require ‘libglade2’

(…other code…)

def my\_method  
@thread\_login = Thread.new {  
begin  
my = Mysql.new(“host”, “user”, “user”, “db”)  
rescue Exception =\> e  
puts e.to\_s  
end  
}  
end

@btn\_ok.signal\_connect(“clicked”){  
my\_method  
}

The problem is that when I’m pressing the button, even if the Thread is  
launched, the GUI freeze until the exception is displayed.

Please, I need some help with that badly.

Thank you.

–  
Best Regards,

Alin

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 11, 2006, 4:26am UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/2 "2006-11-11T04:26:22Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] Ruby-Thread and Gtk” on Fri, 10 Nov 2006  
18:05:41 +0200,  
“Alin P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

What about the following change:

> def my\_method  
> @thread\_login = Thread.new {  
> begin  
> my = Mysql.new(“host”, “user”, “user”, “db”)  
> rescue Exception =\> e  
> puts e.to\_s  
> end  
> }  
> end

def my\_method  
@thread\_login = Gtk.idle\_add do  
begin  
my = Mysql.new(“host”, “user”, “user”, “db”)  
rescue Exception =\> e  
puts e.to\_s  
end  
end  
end

> @btn\_ok.signal\_connect(“clicked”){  
> my\_method  
> }

## Thanks,

kou

* * *

Using Tomcat but need to do more? Need to support web services,  
security?  
Get stuff done quickly with pre-integrated technology to make your job  
easier  
Download IBM WebSphere Application Server v.1.0.1 based on Apache  
Geronimo  
[http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 11, 2006, 10:36am UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/3 "2006-11-11T10:36:54Z")

</div>

Thanks a lot kou,

For your fast feedback, even one minor change is noticed, the GUI still  
is  
freezing until the exception or positive answer from mysql is returned.  
Maybe this is the way, but used together with threads ?!

Once again, thanks, but I have to find other solution.

Alin.

On 11/11/06, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def my\_method  
> > @thread\_login = Gtk.idle\_add do  
> > }  
> > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo  
> > [http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Best Regards,

Alin

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 11, 2006, 10:48am UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/4 "2006-11-11T10:48:55Z")

</div>

It looks like MySQL is blocking… Ruby uses Green Threads, that is -  
cooperative ones. So if one thread calls a blocking call, all threads  
have to wait for it. I find it hard to believe that MySQL actually is  
blocking… could you try this with a non-GTK ruby application?

On 11/11/06, Alin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > What about the following change:
> > 
> > > @btn\_ok.signal\_connect(“clicked”){  
> > > Using Tomcat but need to do more? Need to support web services, security?
> 
> Get stuff done quickly with pre-integrated technology to make your job

–  
Protect your digital freedom and privacy, eliminate DRM, learn more at  
[http://www.defectivebydesign.org/what\_is\_drm](http://www.defectivebydesign.org/what_is_drm)

Ohad L.

* * *

Using Tomcat but need to do more? Need to support web services,  
security?  
Get stuff done quickly with pre-integrated technology to make your job  
easier  
Download IBM WebSphere Application Server v.1.0.1 based on Apache  
Geronimo  
[http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 11, 2006, 11:02am UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/5 "2006-11-11T11:02:58Z")

</div>

Yes, I can try that and this is the result:

Script (the whole script, I think you can try it also):

#!/usr/bin/ruby

# 

require ‘mysql’

$stdout.sync=true  
$stderr.sync=true

class Login  
def initialize  
# ----------- The new Thread ----------  
thread\_login = Thread.new do  
begin  
dbh=Mysql.real\_connect(“80.96.96.142”,  
“alin”, “alin”, “alin”)  
puts “Connection successfull”  
rescue Exception =\> e  
puts e.to\_s  
end  
end  
# ----- The end of the thread --------

```
            # ---- One short iteration with sleep period of 1 sec

```

* * *

```
            10.times do
                    print "."
                    sleep 1
            end
            # ---------- End of iteration ----------------
    end

```

end  
login=Login.new

I think that thread should be launched and no matter how long the  
connection  
will take in order to complete, the dots will be displayed in no time.  
But the dots are not displayed, until the connection will have a  
feedback.

Of course, I tried without the sleep also, and the same thing is  
happening.

What seems to be the problem ?

PS: sorry for the comments, but I wanted to be sure that you understand  
what  
I’m trying to do 🙂

On 11/11/06, Ohad L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> is
> 
> > > Hi,
> > > 
> > > > @thread\_login = Thread.new {  
> > > > begin
> > > 
> > > Get stuff done quickly with pre-integrated technology to make your job
> > 
> > Using Tomcat but need to do more? Need to support web services,  
> > [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Best Regards,

Alin

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 11, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/6 "2006-11-11T14:13:18Z")

</div>

Thanks a lot,

It’s a real real help.

Best regards,

Alin.

On 11/11/06, Ohad L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > # 
> > 
> > ```
> > begin
> > 
> > ```
> > 
> > * * *
> > 
> > I think that thread should be launched and no matter how long the  
> > PS: sorry for the comments, but I wanted to be sure that you understand
> > 
> > > > Once again, thanks, but I have to find other solution.  
> > > > removed\_email\_address@domain.invalid\>
> > > > 
> > > > > > ```
> > > > > > rescue Exception => e
> > > > > > 
> > > > > > ```
> > > > > 
> > > > > ```
> > > > > puts e.to_s
> > > > > 
> > > > > ```
> > > > > 
> > > > > –  
> > > > > easier  
> > > > > [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)
> > 
> > > > 
> > > 
> > > –  
> > > Get stuff done quickly with pre-integrated technology to make your job
> > 
> > Using Tomcat but need to do more? Need to support web services,  
> > [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Best Regards,

Alin

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [November 11, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/ruby-thread-and-gtk/75520/7 "2006-11-11T13:55:14Z")

</div>

Networking calls are probably blocking. A quick google search seems to  
show that this is a known issue… ☹

On 11/11/06, Alin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $stderr.sync=true  
> puts e.to\_s  
> # ---------- End of iteration ----------------
> 
> > have to wait for it. I find it hard to believe that MySQL actually is
> > 
> > > Once again, thanks, but I have to find other solution.  
> > > removed\_email\_address@domain.invalid\>
> > > 
> > > > > ```
> > > > > rescue Exception => e
> > > > > 
> > > > > ```
> > > > 
> > > > ```
> > > > puts e.to_s
> > > > 
> > > > ```
> > > > 
> > > > –  
> > > > Geronimo
> 
> security?
> 
> > > 
> > 
> > Ohad L.  
> > ruby-gnome2-devel-en mailing list  
> > Alin  
> > removed\_email\_address@domain.invalid  
> > [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Protect your digital freedom and privacy, eliminate DRM, learn more at  
[http://www.defectivebydesign.org/what\_is\_drm](http://www.defectivebydesign.org/what_is_drm)

Ohad L.

* * *

Using Tomcat but need to do more? Need to support web services,  
security?  
Get stuff done quickly with pre-integrated technology to make your job  
easier  
Download IBM WebSphere Application Server v.1.0.1 based on Apache  
Geronimo  
[http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)
