如何在特定时间执行循环

时间:2011-05-10 05:24:27

标签: c#

如何在指定时间内执行特定循环

Timeinsecond = 600

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

如何在这里设置时间变量,如果我可以使用Timer对象的启动和停止方法,它不会在几秒钟内返回我的时间

此致 NewDev

7 个答案:

答案 0 :(得分:52)

以下可能会有所帮助:

  Stopwatch s = new Stopwatch();
  s.Start();
  while (s.Elapsed < TimeSpan.FromSeconds(600)) 
  {
      //
  }

  s.Stop();

答案 1 :(得分:23)

如果您想要易用性:

如果您没有很高的准确度要求(真正的毫秒级精度 - 例如每秒写高帧数视频游戏,或类似的实时模拟),那么您可以简单地使用System.DateTime结构:

// 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...
}

TimeSpan.FromMinutes更改为您需要的任何时间段,秒,分钟等。

如果要调用类似Web服务的东西,在短时间内向用户显示内容,或者检查磁盘上的文件,我会专门使用它。

如果您想要更高的准确度:

查看Stopwatch课程,然后查看Elapsed成员。它稍微难以使用,因为你必须启动它,it has some bugs which will cause it to sometimes go negative,但如果你需要真正的毫秒级精度,它会很有用。

使用它:

var stopwatch = new Stopwatch();
stopwatch.Start();

while(stopwatch.Elapsed < TimeSpan.FromSeconds(5))
{
    // Execute your loop here...
}

答案 2 :(得分:0)

创建一个启动,停止和经过时间的函数,如下所示:

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;
    }

}

现在在foreach循环中执行以下操作:

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

答案 3 :(得分:0)

答案 4 :(得分:0)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Accord.Video.FFMPEG;


namespace TimerScratchPad
{
    public partial class Form1 : Form
    {
        VideoFileWriter writer = new VideoFileWriter();
        int second = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            writer.VideoCodec = VideoCodec.H264;
            writer.Width = Screen.PrimaryScreen.Bounds.Width;
            writer.Height = Screen.PrimaryScreen.Bounds.Height;
            writer.BitRate = 1000000;
            writer.Open("D:/DemoVideo.mp4");

            RecordTimer.Interval = 40;
            RecordTimer.Start();

        }

        private void RecordTimer_Tick(object sender, EventArgs e)
        {
            Rectangle bounds = Screen.GetBounds(Point.Empty);
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
                writer.WriteVideoFrame(bitmap);
            }

            textBox1.Text = RecordTimer.ToString();
            second ++;
            if(second > 1500)
            {
                RecordTimer.Stop();
                RecordTimer.Dispose();
                writer.Close();
                writer.Dispose();
            }
        }
    }
}

答案 5 :(得分:-1)

这很丑......但你可以试试这个:

DateTime currentTime = DateTime.Now;
DateTime future = currentTime.AddSeconds(5);
while (future > currentTime)
{
    // Do something here ....
    currentTime = DateTime.Now;
  // future = currentTime.AddSeconds(5);
}

答案 6 :(得分:-3)

而不是如此昂贵的操作,我建议这样做:这是令人讨厌的,但是最好坐下来而不是不必要地加热CPU,这个问题可能是学术性的。

using System.Threading;

Thread.Sleep(600000);