Search code examples
c#wpftimer

Execute a function until 5 minutes in WPF


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.


Solution

  • 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();