簡體   English   中英

如何檢查特定時間是否過去

[英]How to check if specific time has passed

我開始操作,命令行應用程序獲取的參數之一是Number ,它代表我的操作需要運行多少時間。

int duration = int.Parse(args[0]) // run my operation for this time in minutes

這是我嘗試過的(不起作用):

...
DateTime start = DateTime.Now;
// check whether this time in minutes passed
if (start.Minute > duration )
    break;

編輯:使用UtcNow

    void DoWork(int durationInMinutes)
    {
        DateTime startTime = DateTime.UtcNow;
        TimeSpan breakDuration = TimeSpan.FromMinutes(durationInMinutes);

        // option 1
        while (DateTime.UtcNow - startTime < breakDuration)
        {
            // do some work
        }

        // option 2
        while (true)
        {
            // do some work
            if (DateTime.UtcNow - startTime > breakDuration)
                break;
        }
    }

為此,您可以使用StopWatch通過定義

StopWatch watch = new StopWatch();
watch.Start();

以及您的代碼完成寫入的位置

watch.Stop();

通過使用stopwach,您可以詳細了解應用程序的運行時間。 當然,如果我理解你是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM