2011-05-10 25 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