1

Possible Duplicate:
How to execute the loop for specific time

So, I have an infinite loop in C#, but the thing is, I want the loop to end at some point. Not based on a count or a loop interval, but based on a period of time, so once that period of time has passed, the loop ends. How do I do such a thing?

Community
  • 1
  • 1
Alper
  • 1
  • 12
  • 39
  • 78

1 Answers1

3

Make use of Timer or Thread to do that. you can achieve the task easily.

System.Threading.Timer Timer;
System.DateTime StopTime;
public void Run()
{
    StopTime = System.DateTime.Now.AddMinutes(10);
    Timer = new System.Threading.Timer(TimerCallback, null, 0, 5000);
}

private void TimerCallback(object state)
{
    if(System.DateTime.Now >= StopTime)
    {
            Timer.Dispose();
            return;
    }
    // do your work.....
}

check other solution : .NET, event every minute (on the minute). Is a timer the best option?

or Edit

try

          DateTime dt = DateTime.Now.AddMinutes(10);

            for (; ; )
            {
                if (dt < DateTime.Now)
                    break;
            }
Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263