2011-05-10 35 views
17

我怎么能执行一个particluar环路指定的时间如何执行循环的具体时间

Timeinsecond = 600 

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

如何设置时间varaible这里,如果我可以使用Timer对象的启动和停止方法,它多年平均值还给我时间,以秒

问候 NewDev

回答

15

如果你想易用性:

如果你没有强大的精度要求(真毫秒级精确度 - 比如写每秒的视频游戏,或类似的实时仿真高帧),那么你可以简单地使用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... 
} 
+9

'DateTime.Now'很慢(我的约1000ns机)。您应该使用'DateTime.UtcNow',因为它不需要执行时区转换,所以速度提高了大约100倍。 – Gabe 2011-05-10 05:41:25

+1

@加贝:很好的信息。感谢教我一些东西:)编辑答案... – 2011-05-10 05:43:17

38

可能是以下帮助:

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

    s.Stop(); 
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(); 
-2

这是丑陋的....但你可以试试这个:

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

为什么downvotes? – 2017-06-04 21:12:21

-2

而是如此昂贵的操作,我建议这样的:

using System.Threading; 

Thread.Sleep(600000); 
+0

不是一个好主意,让主线睡觉。浪费资源,根本不是一个好的做法。 – 2016-11-04 00:32:20

+0

我toatally同意,但它是更好地坐等比跑步浪费资源,这个问题本身也许是学术。 – 2017-11-24 21:47:08

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

该代码将运行而不会消耗更多的系统资源多,绝不会出现放缓系统崩溃,当您运行while循环 这个代码将屏幕截图和写入视频文件每40毫秒即24FPS像 – 2018-02-04 06:25:29