I have a function that I want to allow to run for a given length of time and then, if it hasn't quit on it's own, abort. What is the best way to do this?
The best I have thought of would be to run it in another thread, wait with a timeout for it to die and then use Thread.Abort()
to kill it (this might not work if the function has the wrong kind of catch
block). Another option (one that I don't know how to make work) would be some kind of preemptive timer with a throw in it.
Is there a better way? Some kind of easy sandbox system?
Edit: the function I'm going to be running doesn't have any system to check if it should cancel and I can't (as in must not) add it. Also, this a a test harness of sorts so the condition under which I will be killing the function is that it has run amuck. In that case I can't count on it doing anything correctly.