How to execute the loop for specific time

后端 未结 7 1031
心在旅途
心在旅途 2020-12-05 13:37

How can i execute the a particluar loop for specified time

Timeinsecond = 600

int time = 0;
while (Timeinsecond > time)
{
   // do something here
}


        
相关标签:
7条回答
  • 2020-12-05 14:02

    Create a function for starting, stopping, and elapsed time as follows:

    Class CustomTimer
    { 
       private DateTime startTime;
        private DateTime stopTime;
        private bool running = false;
    
        public void Start() 
        {
            this.startTime = DateTime.Now;
            this.running = true;
        }
    
        public void Stop() 
        {
            this.stopTime = DateTime.Now;
            this.running = false;
        }
    
        //this will return time elapsed in seconds
        public double GetElapsedTimeSecs() 
        {
            TimeSpan interval;
    
            if (running) 
                interval = DateTime.Now - startTime;
            else
                interval = stopTime - startTime;
    
            return interval.TotalSeconds;
        }
    
    }
    

    Now within your foreach loop do the following:

        CustomTimer ct = new CustomTimer();
        ct.Start();
        // put your code here
        ct.Stop();   
      //timeinsecond variable will be set to time seconds for your execution.
      double timeinseconds=ct.GetElapsedTime();
    
    0 讨论(0)
  • 2020-12-05 14:09

    Instead of such an expensive operation I'd recommend this: It's nasty but it's better to sit than running for doing nothing heating the cpu unnecesarily, the question is may be academic.

    using System.Threading;
    
    Thread.Sleep(600000);
    
    0 讨论(0)
  • 2020-12-05 14:16

    May be the following will help:

      Stopwatch s = new Stopwatch();
      s.Start();
      while (s.Elapsed < TimeSpan.FromSeconds(600)) 
      {
          //
      }
    
      s.Stop();
    
    0 讨论(0)
  • 2020-12-05 14:16

    It's ugly .... but you could try this:

    DateTime currentTime = DateTime.Now;
    DateTime future = currentTime.AddSeconds(5);
    while (future > currentTime)
    {
        // Do something here ....
        currentTime = DateTime.Now;
      // future = currentTime.AddSeconds(5);
    }
    
    0 讨论(0)
  • 2020-12-05 14:21

    If you want ease of use:

    If you don't have strong accuracy requirements (true millisecond level accuracy - such as writing a high frames per second video game, or similar real-time simulation), then you can simply use the System.DateTime structure:

    // Could use DateTime.Now, but we don't care about time zones - just elapsed time
    // Also, UtcNow has slightly better performance
    var startTime = DateTime.UtcNow;
    
    while(DateTime.UtcNow - startTime < TimeSpan.FromMinutes(10))
    {
        // Execute your loop here...
    }
    

    Change TimeSpan.FromMinutes to be whatever period of time you require, seconds, minutes, etc.

    In the case of calling something like a web service, displaying something to the user for a short amount of time, or checking files on disk, I'd use this exclusively.

    If you want higher accuracy:

    look to the Stopwatch class, and check the Elapsed member. It is slightly harder to use, because you have to start it, and it has some bugs which will cause it to sometimes go negative, but it is useful if you need true millisecond-level accuracy.

    To use it:

    var stopwatch = new Stopwatch();
    stopwatch.Start();
    
    while(stopwatch.Elapsed < TimeSpan.FromSeconds(5))
    {
        // Execute your loop here...
    }
    
    0 讨论(0)
  • 2020-12-05 14:22

    use Timers in c# http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

    0 讨论(0)
提交回复
热议问题