Running sections of code at specific time intervals?


#1

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


#2

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-The-Main-Event-Loop.html#g-timeout-add

±[ Gergely K. 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


#3

On Mon, Dec 22, 2008 at 14:12, KONTRA, Gergely removed_email_address@domain.invalid
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


#4

On Mon, Dec 22, 2008 at 7:41 PM, Dobai-Pataky Bálint removed_email_address@domain.invalid
wrote:

use
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
https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en


Guillaume C. - http://zarb.org/~gc/


#5

use
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.


#6

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

On Mon, Dec 22, 2008 at 7:41 PM, Dobai-Pataky Bálintremoved_email_address@domain.invalid wrote:

use
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.


#7

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.


#8

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.