如何在特定时间执行循环

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

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

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

关心NewDev

可能以下将有所帮助:

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

如果你想要易用性:

如果您没有很高的精度要求(真正的毫秒级精度 – 例如每秒编写高帧数video游戏,或类似的实时模拟),那么您可以简单地使用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成员。 它稍微难以使用,因为你必须启动它,并且它有一些错误会导致它有时变为负数 ,但如果你需要真正的毫秒级精度,那么它很有用。

要使用它:

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

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

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

在c#中使用计时器http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

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

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

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

而不是这么昂贵的操作,我建议这样做:这是令人讨厌的,但最好坐下来,而不是无所事事地加热cpu,这个问题可能是学术性的。

 using System.Threading; Thread.Sleep(600000);