0

I would like to stop a function after a fixed execution time. Here i've written an sample for you:

                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    while (timer.Elapsed.TotalSeconds < timeLimite)
                    {
                       //my functions
                    }
                    timer.Stop();

In my own test, it does not limit the execution time inside my functions. In other words, it tests the stopping criterion before and after the execution of my functions but does not interrupt the algorithm inside "my functions".

  • 2
    It only checks the condition (in the while) whenever it finished everything inside. So it will always fully complete the code inside if it gets in. – EpicKip Dec 19 '17 at 12:06
  • Your functions need to be aware of the time limit and exit when they exceed their time - you can't force a function to exit. – Enigmativity Dec 19 '17 at 12:08
  • You would need to check *between* (or within) calls to whatever "my functions" represents, as you have discovered a loop is logically not going to work. – Alex K. Dec 19 '17 at 12:08
  • 2
    @ArijitMukherjee - What is a "recursive thread"? – Enigmativity Dec 19 '17 at 12:08
  • yes, i would like to know if there is a way to stop the algorithm inside... – Marvel LePont Dec 19 '17 at 12:09
  • 1
    @Progman - That's an awful accepted answer on the duplicate you put forward. Calling `Thread.Abort()` should almost never be used and it certainly shouldn't be used in this case. – Enigmativity Dec 19 '17 at 12:10
  • @Enigmativity He wants to abort a set of functions even if its still busy. If you ask me `Thread.Abort()` is perfect for this. The idea to abort functions is bad to begin with, but for that purpose, thread.abort should be fine. – EpicKip Dec 19 '17 at 12:13
  • You can run your function in a [task and cancel it it's taking too long.](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/cancel-async-tasks-after-a-period-of-time) – Zohar Peled Dec 19 '17 at 12:13
  • @EpicKip - No, it's not fine. It can be disastrous. The only time it is safe to use `Thread.Abort()` is when trying to forcibly exit your entire app. – Enigmativity Dec 19 '17 at 12:14
  • @ZoharPeled - That only works if the called code respects the cancellation token in some way. The OP's code could simply respect the `Stopwatch` and achieve the same result. – Enigmativity Dec 19 '17 at 12:16
  • @Enigmativity Interrupting an algorithm running forever (while) isn't the best idea to begin with. And likely this isn't production code, I wouldn't think it could hurt (as an exercise at least) – EpicKip Dec 19 '17 at 12:17
  • What exactly does "my functions" do that takes time? – Magnus Dec 19 '17 at 12:19
  • @PhdStudent - If you let us know what `//my functions` look like we might be able to offer a **safe** way to abort the code. – Enigmativity Dec 19 '17 at 12:20
  • Just to be explicit: `while (i++<100) { /* code runs again and again 100 times */ }`. With a timer in the loop: `while (timer.Elapsed< '2 minutes') { /* code runs again and again for two minutes */ }`. So really, it doesn't do what you thought at all, and can make your code run thousands of times... – Kobi Dec 19 '17 at 12:27
  • You are asking how to write a time sliced thread scheduler. That's some fun OS level work :) – Michael Dorgan Dec 19 '17 at 23:21
  • Did you read all answers to this question: https://stackoverflow.com/questions/7413612/how-to-limit-the-execution-time-of-a-function-in-c-sharp – sahar Dec 20 '17 at 13:42

0 Answers0