-__-" hehe why did I not let Hayoo or Hoogle help me there *sigh*<br><br>Thanks!!<br><br><div class="gmail_quote">2009/5/31 Sterling Clover <span dir="ltr"><<a href="mailto:s.clover@gmail.com">s.clover@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">The proper way is just to wrap System.Timeout, which does some rather clever things with regards to exception semantics. The code for it is a joy to read, by the way.<br>
<br>
--S.<div><div></div><div class="h5"><br>
<br>
On May 30, 2009, at 5:36 PM, Sebastian Sylvan wrote:<br>
<br>
</div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">
<br>
<br>
On Sat, May 30, 2009 at 10:32 PM, Cetin Sert <<a href="mailto:cetin.sert@gmail.com" target="_blank">cetin.sert@gmail.com</a>> wrote:<br>
Thank you for your reply, I'd come up with the following:<br>
<br>
timed :: Int → IO a → b → IO (Either b a)<br>
timed max act def = do<br>
<br>
r ← new<br>
<br>
t ← forkIO $ do<br>
a ← act<br>
r ≔ Right a<br>
<br>
s ← forkIO $ do<br>
wait max<br>
e ← em r<br>
case e of<br>
True → do<br>
kill t<br>
r ≔ Left def<br>
<br>
False → return ()<br>
<br>
takeMVar r<br>
<br>
---------------------<br>
<br>
*Network.Port.Scan> timed 500 (wait 50000 >> return 0) 'x'<br>
Left 'x'<br>
*Network.Port.Scan> timed 500000 (wait 50000 >> return 0) 'x'<br>
Right 0<br>
<br>
---------------------<br>
<br>
before reading your reply:<br>
<br>
<br>
timed timeout act fallback = do<br>
res <- newEmptyMVar<br>
tid <- forkIO $ act >>= writeMVar res<br>
threadDelay timeout<br>
stillRunning <- isEmptyMVar res<br>
if stillRunning then killThread tid >> return fallback else takeMVar res<br>
<br>
---------------------<br>
<br>
*Network.Port.Scan> timed2 500 (wait 50000 >> return 0) 'x'<br>
<br>
<interactive>:1:33:<br>
No instance for (Num Char)<br>
arising from the literal `0' at <interactive>:1:33<br>
Possible fix: add an instance declaration for (Num Char)<br>
In the first argument of `return', namely `0'<br>
In the second argument of `(>>)', namely `return 0'<br>
In the second argument of `timed2', namely<br>
`(wait 50000 >> return 0)'<br>
<br>
<br>
Right, I forgot about the "Either" bit so you'd have to make sure the action's result and the default has the same type (or modify it to return an Either).<br>
<br>
<br>
<br>
-- <br>
Sebastian Sylvan<br>
+44(0)7857-300802<br>
UIN: 44640862<br></div></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>