 
 
 
 
 
 
Re: How to catch interrupts generated by TimeConstrained?
- To: mathgroup at smc.vnet.net
- Subject: [mg121778] Re: How to catch interrupts generated by TimeConstrained?
- From: Peter Pein <petsie at dordos.net>
- Date: Sat, 1 Oct 2011 03:09:26 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j6123m$m2q$1@smc.vnet.net>
Am 29.09.2011 08:09, schrieb Alexey Popkov:
> Hello,
> 
> Mathematica has the CheckAbort function which allows to catch and
> handle user-generated and programmatic Aborts. But it does not allow
> to catch interrupts generated by such functions as TimeConstrained and
> MemoryConstrained:
> 
> TimeConstrained[CheckAbort[Pause[100], Print["From CheckAbort"]], 1]
> 
> (does not print "From CheckAbort").
> 
> Is there a way to catch such interrupts in Mathematica?
> 
> Alexey
> 
> P.S. The reason why I need this is that I have a function that creates
> MathLink objects which must be closed in the case of any interrupts or
> aborts but not in other cases.
> 
> 
Well if CheckAbort is needed for other reasons (e.g. impatient user),
try the workaround
If[CheckAbort[
     TimeConstrained[Beep[]; Pause[2], #], $Aborted] === $Aborted,
   "closing links...", "keeping links open"] & /@ {1, 3}
if you do not touch the keyboard while running this toy example it will
end up with {"closing links...", "keeping links open"}
and if you abort after the second beep using <Alt>. or whatever is the
abort-shortcut on your system then it will return
{"closing links...", "closing links..."}
hth,
Peter

