-3

In a WPF form I'm calling doSomeFunction() method in a while loop. This will run until x == y which works properly.

while (x != y)
{
   doSomeFunction();
}

I need to add additional functionality, which needs to check this condition until 5 minutes only. After the 5 minutes if x != y I need to return.

Hille
  • 2,123
  • 22
  • 39
Tom
  • 1,343
  • 1
  • 18
  • 37
  • Possible duplicate of [Calling a method every x minutes](https://stackoverflow.com/questions/13019433/calling-a-method-every-x-minutes) – xdtTransform Apr 18 '19 at 06:12
  • Not a duplicate. Please read the question. Here I'm asking to call method for a 5 minutes. But in your link showing answer for call a method in each 5 mins again and again. – Tom Apr 18 '19 at 06:43
  • well you are right that's not the correct duplicate. Maybe i got lost into all the tab I got from the google search. [look](https://social.msdn.microsoft.com/Forums/vstudio/en-US/fb6b6f4f-ae2f-44f4-9b31-8ac92b311049/exit-while-loop-after-x-minutes?forum=csharpgeneral), let me find the equivalent of this on Stackoverflow. So I will be damned if t was not a dupe. Dupe are not bad things. If you didn't find the solution while looking for it. One must make the already existing solution more visible so anyone could find it. – xdtTransform Apr 18 '19 at 06:53

3 Answers3

3

Much cleaner and clear approach, You can also make use of stopwatch

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
while (x != y && stopwatch.Elapsed.Minutes <=5)
{
  doSomeFunction();
 }
stopWatch.Stop();
Justin CI
  • 2,693
  • 1
  • 16
  • 34
Vikas Gupta
  • 1,293
  • 1
  • 15
  • 21
2

You can take the EnterTime time and check

 DateTime EnterTime = DateTime.Now;
 while (x != y && EnterTime.AddMinutes(5) >= DateTime.Now)
  {
      doSomeFunction();
   }

If both conditions are true then loop will execute

Avinash Reddy
  • 937
  • 8
  • 22
0

You can also use the Task library and specify a timeout.

    Task.Factory.StartNew(() =>
    {
        while (x != y)
        {
            // if actions are being performed on UI controls then please do invoke
            Console.Write("test");
        }
    }).Wait(millisecondsTimeout:30000);
peeyush singh
  • 1,337
  • 1
  • 12
  • 23
  • It will run for 30000 millisec even though the condition is false. Hence it is not the correct solution – Vikas Gupta Apr 18 '19 at 09:10
  • @VikasGupta true, on the other hand in the case your answer the time condition is useless, since in case x==y the loop will exit straightaway, and there is no in between code that might delay checking the condition. Also user did state that he want to check the condition until 5 mins. I think there is a bit of ambiguity in the question, though since the user has accepted your answer, it must be the right functionality....just that the timestamp check makes no sense if x==y, the only place it works is when x!=y initially and then switches to x==y, in that case there is a difference.... – peeyush singh Apr 19 '19 at 03:06
  • ahh got it now, yeah will update the answer with a cancellable task, but agreed your answer is much simpler then,.....also from comments the user is already on the background thread. – peeyush singh Apr 19 '19 at 03:14