How can I abort the execution of a method if its taking too long to run?
e.g.
string foo = DoSomethingComplex();
but if DoSomethingComplex() is taking too long (20 seconds lets say).
then just set foo to “”;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can create a new thread running your method, then:
Join returns true if thread finished successfully before designated time. Obviously, would not abort the method (probably no way to do that directly), but the thread — but it seems that would accomplish what you desired.
A simple threading code example probably would go something like below. Note I made some assumptions about signature with parameter, all this in some class, etc. For method:
and then in the same class:
usage aka code in some other method in ClassFooIsIn:
Foo should be initialized before above shown usage, so in reality you could possibly skip foo = “”; line.