# Can I force a function to quit and return some value after a certain amount of time has passed during its evaluation?

Imagine I provide some random input to function like FindInstance[], and I observe that, despite the existence of good solutions, the function will, with some probability, indefinitely evaluate without success. For an example, please see my earlier question: Why is FindInstance failing when I relax a set of constraints?

Can I somehow force the function to return, say, an empty list {} after some time $T$ has passed?

-
Did you check TimeConstraint? –  Spawn1701D Apr 22 '13 at 1:11
@Spawn1701D That seems not to work for FindInstance? –  Peter Apr 22 '13 at 1:13
@Spawn1701D I'm looking for something I can also wrap around some section of code to force it to restart or quit after some time has passed. –  Peter Apr 22 '13 at 1:14
There is also the wrapper/Function variant TimeConstrained. –  Spawn1701D Apr 22 '13 at 1:16
@Spawn1701D Yup, that seems to work! –  Peter Apr 22 '13 at 1:17

TimeConstrained[expr, t, failexpr] returns failexpr if the time constraint t is not met.