# Running sections of code at specific time intervals?

**URL:** <https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185>\
**Category:** Ruby-Gnome 2\
**Created:** [December 22, 2008, 11:47am UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185 "2008-12-22T11:47:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 22, 2008, 11:47am UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/1 "2008-12-22T11:47:02Z")

</div>

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. 😃  
(please bear in mind I’m a complete noob at programming)

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 22, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/2 "2008-12-22T14:13:09Z")

</div>

Hi!

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

Well, not documented…  
so look for the original c version, like this

> **[GLib-2.0](https://docs.gtk.org/glib/)**
>
> Reference for GLib-2.0

±[Gergely K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ]------------------+  
| |  
| 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

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 22, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/3 "2008-12-22T17:30:23Z")

</div>

On Mon, Dec 22, 2008 at 14:12, KONTRA, Gergely [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi!
> 
> Maybe you are looking for  
> [http://ruby-gnome2.sourceforge.jp/hiki.cgi?GLib%3A%3ATimeout](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

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 23, 2008, 9:00am UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/4 "2008-12-23T09:00:59Z")

</div>

On Mon, Dec 22, 2008 at 7:41 PM, Dobai-Pataky Bálint [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> use  
> [http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk&key=timeout\_add#Gtk.timeout\_add](http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk&key=timeout_add#Gtk.timeout_add)  
> 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  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Guillaume C. - [NVC Communication Obstacles Game](http://zarb.org/~gc/)

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 22, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/5 "2008-12-22T19:42:29Z")

</div>

use  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk&key=timeout\_add#Gtk.timeout\_add](http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk&key=timeout_add#Gtk.timeout_add)  
it runs ona background thread, that’s what you want.

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 23, 2008, 10:21am UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/6 "2008-12-23T10:21:55Z")

</div>

On 12/23/2008 10:00 AM, Guillaume C. wrote:

> On Mon, Dec 22, 2008 at 7:41 PM, Dobai-Pataky BÃ¡lint[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > use  
> > [http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk&key=timeout\_add#Gtk.timeout\_add](http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gtk&key=timeout_add#Gtk.timeout_add)  
> > 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.

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 24, 2008, 8:53am UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/7 "2008-12-24T08:53:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dawson360](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@dawson360](https://www.ruby-forum.com/u/dawson360)\
**Post date:** [December 24, 2008, 2:01am UTC](https://www.ruby-forum.com/t/running-sections-of-code-at-specific-time-intervals/156185/8 "2008-12-24T02:01:19Z")

</div>

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.
