Possible Duplicate:
Set timeout to an operation
How can i set timeout for a line of code in c#. For example RunThisLine(SomeMethod(Some Input), TimeSpan.FromSeconds(10))
run SomeMethod
with 10 second time out. Thanks in advance.
setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
You can use the Task Parallel Library. To be more exact, you can use Task.Wait(TimeSpan)
:
using System.Threading.Tasks; var task = Task.Run(() => SomeMethod(input)); if (task.Wait(TimeSpan.FromSeconds(10))) return task.Result; else throw new Exception("Timed out");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With