From: Zhukov Pavel Date: 2008-06-30T18:35:29+09:00 Subject: Re: Sleep in a multithreaded environment On Mon, Jun 30, 2008 at 1:07 PM, Me Me wrote: > Hi, > I'm trying to control an external process I'm running and quit it in > case it got > stuck. > To do that I have to check that the external exe keeps writing on > stdout, if it doesn't update it for a while I'll quit it. > I came up with the following code (using threads) but the sleep in the > main thread blocks everything. (The same code works under Mac OSX) > Is there something I'm doing wrong? > Or a better way to achieve what I need? > Thanks a lot in advance > > n = 0 > t1 = Thread.new > { > begin > IO.popen("executable.exe") do |pipe| > pipe.each("\r") do |line| > puts line > n += 1 > end > end > end > } > > k = 0 > while t1.alive? > sleep(10) # DOESN'T SEEM TO WORK ON WINDOWS > if k == n > puts "DEAD" > t1.raise("close") > end > k = n > end > -- > Posted via http://www.ruby-forum.com/. > > pipe=IO.popen("executable.exe") io=select([io],nil,nil,10) #10 - timeout if io==nil puts "DEAD" t1.raise("close") end