0

I would like to repeat a function until the function has no timeout. The function should return something but if it takes too long the function call should be repeated. This is what I have:

var task = Task.Run(() => main.getPlaces(field));

while (true)
    {
        if (task.Wait(TimeSpan.FromSeconds(1)))
        {
            field = task.Result;
            break;
        } else
        {
            task = Task.Run(() => main.getPlaces(field));
            continue;
        }
    }

It seems like the function isn't recalled with this setup. The console output just gets slower and slower.

Julian
  • 1,380
  • 12
  • 28
  • 2
    *"This is what I have"* - okay, and what's the problem? Does it give errors? Not compile? Not do what you wanted? – Matt Burland May 22 '17 at 16:59
  • I added a little explanation. – Julian May 22 '17 at 17:01
  • Of what? It's your code. – Matt Burland May 22 '17 at 17:02
  • My current scenario. – Julian May 22 '17 at 17:02
  • *"The execution just gets slower and slower."* - depending on what it's doing, unless `getPlaces` is actually being cancelled, it may still be running. Just because you've stopped waiting for it, doesn't automatically mean the task has stopped running. See [here](https://stackoverflow.com/a/4036260/1250301) for example. – Matt Burland May 22 '17 at 17:03
  • I never said it stop running. The output just slows down and I can't interpret the result furthermore because my C# knowledge is very limited. – Julian May 22 '17 at 17:06
  • You start `getPlaces`, you wait 1 second and then start it again. Then you wait 1 more second and start it again...and so on. Depending on how long `getPlaces` actually takes, you could have dozens of instances of the task running *at the same time*. No wonder it's getting slow. – Matt Burland May 22 '17 at 17:08
  • I assume you are not cancelling the old task upon calling the new task. It might be creating multiple tasks. Warning: Haven't tested by executing. I would try this way https://stackoverflow.com/a/4036260/4052549 – Ram's stack May 22 '17 at 17:09
  • He's calling the function from within itself, and for no apparent reason. Julian, what exactly are you trying to do with this code? a simple while loop would do what you actually described. – Trey May 22 '17 at 17:11
  • @Trey, `getPlaces` could be endless. That's why I would like to catch this case and restart it until it has an end. This approach is horrible, I know, but it would be enough for my purposes. – Julian May 22 '17 at 17:14
  • Then you need to STOP `getPlaces` - which would require you to start the task with a cancellation source. Cancel the task after the wait expires without getting a result. And then in your `getPlaces` function actually respond to the cancellation and stop doing whatever it's doing. – Matt Burland May 22 '17 at 17:18
  • can you please tell me what function the while(true) is in? – Trey May 22 '17 at 17:18
  • `static void Main(string[] args)` in class `Program`. Complete code: https://pastebin.com/g2snC1bh – Julian May 22 '17 at 17:19
  • Thanks. SO walk me through what you want, and I'll get you there? – Trey May 22 '17 at 17:22
  • I would like to restart the method until the method has no timeout because it's internal calculation would be endless. – Julian May 22 '17 at 17:25
  • Ok thanks @Julian, if you put break points in, what is not working? Your code/idea sounds solid(not an approach I have ever used...) But without debugging on my end I need more information about what is failing. – Trey May 22 '17 at 18:53

0 Answers0