gets() -- Need to skip when no response after 20seconds

R

Raveendran .P

Code:

puts "Print your age"
a=gets()
puts "Timeout. 20 Seconds Gone. Please re-run the Applciation"


Need:

a=gets() is waiting for input. I need to wait only 20 seconds. If therre
is no input for 20 seconds then script should continue the next line


Thanks
Raveendran P
http://raveendran.wordpress.com
 
F

F. Senault

Le 14 juillet à 08:22, Raveendran .P a écrit :
Need:

a=gets() is waiting for input. I need to wait only 20 seconds.

Look at the Timeout standard library :

begin
Timeout.timeout(20) do
a = gets()
end
rescue Timeout::Error
puts "Timeout. 20 seconds gone, etc"
end

Fred
 
B

Brian Candler

Raveendran said:
Code:

puts "Print your age"
a=gets()
puts "Timeout. 20 Seconds Gone. Please re-run the Applciation"


Need:

a=gets() is waiting for input. I need to wait only 20 seconds. If therre
is no input for 20 seconds then script should continue the next line


Thanks
Raveendran P
http://raveendran.wordpress.com

require 'timeout'
a = nil
begin
puts "Print your age"
Timeout.timeout(20) do
a = gets
end
rescue Timeout::Error
puts "Timeout. 20 Seconds Gone. Please re-run the Applciation"
end

# Warning: like most programs, this may not work as desired under
Windows
 
R

Raveendran .P

Hi Brain and Senault,

The script is still waiting for input from User.

** But the same timeout code works for while loop printing up to 1000.

I am using Windows XP Service pack 2, Ruby 1.8.7

Thanks
 
H

Heesob Park

Hi,

2010/7/14 Raveendran .P said:
Hi Park,



But my environment is Ruby 1.8.7. I need to implement this particular
task in existing application.
Try this code:

require 'Win32API'

@@kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')

for i in 0..19
unless @@kbhit.call.zero?
a = gets()
break
end
sleep 1
end
if i==19
puts "Timeout. 20 Seconds Gone. Please re-run the Applciation"
end


Regards,
Park Heesob
 
M

Michael Fellinger

Hi Park,



But my environment is Ruby 1.8.7. I need to implement this particular
task in existing application.

if select([$stdin], nil, nil, 20)
p gets
end
 
A

Ammar Ali

[Note: parts of this message were removed to make it a legal post.]

if select([$stdin], nil, nil, 20)
p gets
end
IIRC, the last time I tried that on Windows (a while back) the select
returned on the first char and then gets acted as usual, blocking until EOL.
I would like to know if that actually works.

Ammar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top