var stopwatch = new Stopwatch(); stopwatch.Start(); while(stopwatch.Elapsed < TimeSpan.FromSeconds(5)) { // Execute your loop here... }