Forum: Ruby-Gnome 2 Running sections of code at specific time intervals?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cfb681c8506757b099c46813f8f1c4c2?d=identicon&s=25 Danial Dawson (dawson360)
on 2008-12-22 11:47
Is there any way to execute specific code over and over, say every 5
seconds?

I'm creating a little instant messenger program that uses blowfish and
rsa public/private key encryption for secure communication. When a
message is entered it is echoed into a file called text.txt, encrypted
and sent to whoever you are connected to. The problem is that in order
to run the code that receives the message, decrypts it and puts it in
the text view, I've had to implement a temporary solution of pressing a
'get messages' button.

I'm currently working on a solution using another script to check for
messages, decrypt them and save them in a file then running its self
again using 'exec'.

I just need to figure out how to have my main script check this text
file every 5 seconds or so and run the code to display it on the screen.

Any help would be greatly appreciated. :D
(please bear in mind I'm a complete noob at programming)
7988f36d0ee9050e8f1d646db55a9eb0?d=identicon&s=25 KONTRA, Gergely (Guest)
on 2008-12-22 14:13
(Received via mailing list)
Hi!

Maybe you are looking for
http://ruby-gnome2.sourceforge.jp/hiki.cgi?GLib%3A%3ATimeout

Well, not documented...
so look for the original c version, like this
http://library.gnome.org/devel/glib/stable/glib-Th...

+-[ Gergely Kontra <pihentagy@gmail.com> ]------------------+
|                                                           |
| Mobile:(+36 20)356 9656                                   |
|                                                           |
+- "Olyan lángész vagyok, hogy poroltóval kellene járnom!" -+



On Mon, Dec 22, 2008 at 11:47 AM, Danial Dawson
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2008-12-22 17:30
(Received via mailing list)
On Mon, Dec 22, 2008 at 14:12, KONTRA, Gergely <pihentagy@gmail.com>
wrote:
> Hi!
>
> Maybe you are looking for
> http://ruby-gnome2.sourceforge.jp/hiki.cgi?GLib%3A%3ATimeout

Or this:

module Kernel
  def every(seconds)
    loop
      sleep seconds
      yield
    end
  end
end

then just run it as

every 5 do
  puts "five seconds have passed"
end

Of course, that's more or less useless if you don't use it in a
thread.  And, of course, there's no guarantee that it will be fired
after exactly five seconds have passed, so it should be

every 5 do
  puts "at least five seconds have passed"
end
8e6374c0ef7b66942f34798e31edf014?d=identicon&s=25 Dobai-Pataky Bálint (Guest)
on 2008-12-22 19:42
(Received via mailing list)
Attachment: dpblnt.vcf (162 Bytes)
use
http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=vie...
it runs ona background thread, that's what you want.
C838db9af1d90a87a828ecad8556b804?d=identicon&s=25 Guillaume Cottenceau (Guest)
on 2008-12-23 09:00
(Received via mailing list)
On Mon, Dec 22, 2008 at 7:41 PM, Dobai-Pataky Bálint <dpblnt@gmail.com>
wrote:
> use
> 
http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=vie...
> it runs ona background thread, that's what you want.

it doesn't run a background thread. it's called by gtk's main loop,
from the main thread.

> the text view, I've had to implement a temporary solution of pressing a
> (please bear in mind I'm a complete noob at programming)
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> ruby-gnome2-devel-en mailing list
> ruby-gnome2-devel-en@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ruby-...
>
>



--
Guillaume Cottenceau - http://zarb.org/~gc/
8e6374c0ef7b66942f34798e31edf014?d=identicon&s=25 Dobai-Pataky Bálint (Guest)
on 2008-12-23 10:21
(Received via mailing list)
Attachment: dpblnt.vcf (162 Bytes)
On 12/23/2008 10:00 AM, Guillaume Cottenceau wrote:
> On Mon, Dec 22, 2008 at 7:41 PM, Dobai-Pataky Bálint<dpblnt@gmail.com>  wrote:
>
>> use
>> 
http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=vie...
>> it runs ona background thread, that's what you want.
>>
>
> it doesn't run a background thread. it's called by gtk's main loop,
> from the main thread.
>
you are right, sorry.
Cfb681c8506757b099c46813f8f1c4c2?d=identicon&s=25 Danial Dawson (dawson360)
on 2008-12-24 02:01
I've got it working using timeout_add, it doesn't run on a background
thread but it works fine, I've got it refreshing messages and connection
attempts every 3 seconds. The program freezes for a fraction of second
when I've used 'sleep' but I can live with that for now so long as I
have basic functionality :D.

took me a while to figure out how to use timeout but at least I've got a
relatively better command of the ruby language now. I appreciate your
help, cheers guys.
8e6374c0ef7b66942f34798e31edf014?d=identicon&s=25 Dobai-Pataky Bálint (Guest)
on 2008-12-24 08:53
(Received via mailing list)
Attachment: dpblnt.vcf (162 Bytes)
why do you need sleeping?
let timeout_add call your check_messages and display_messages every 3
seconds, don't sleep within it.
return true to get called again.
This topic is locked and can not be replied to.