亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 C# 中使用秒表定期檢查 5 秒

在 C# 中使用秒表定期檢查 5 秒

C#
素胚勾勒不出你 2021-07-05 16:53:16
我想在 C# 中定期檢查 60 秒。我可以通過定期檢查日期來做到這一點,如下所示但是,當我使用秒表時,秒表會重置回開始位置,而不是從上次停止時繼續。using System;using System.Diagnostics;using System.Threading;namespace StopwatchExample{    class Program    {        static void Main()        {            ////Works            //DateTime start = DateTime.Now;            //while (true)            //{            //    DateTime current = DateTime.Now;            //    var diffInSeconds = (current - start).TotalSeconds;            //    if (diffInSeconds > 5)            //    {            //        Console.WriteLine("5 s done!");            //        break;            //    }            //}            // Does not work            Stopwatch stopwatch = new Stopwatch();            stopwatch.Start();            while (true)            {                stopwatch.Stop();                if (stopwatch.Elapsed.Seconds > 5)                {                    Console.WriteLine("5 s done!");                    break;                }                else                {                    stopwatch.Start();                }            }            Console.ReadLine();        }    }}
查看完整描述

2 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

嘗試使用 Timer (System.Timers) 而不是 Stopwatch。設置所需的時間間隔并對 Elapsed 事件執行必要的操作。


在這里您可以了解更多信息。


例子:


public static void Main()

{

    // Create a timer and set a two second interval.

    aTimer = new System.Timers.Timer();

    aTimer.Interval = 2000; // 2000ms == 2s


    // Hook up the Elapsed event for the timer. 

    aTimer.Elapsed += OnTimedEvent;


    // Have the timer fire repeated events (true is the default)

    aTimer.AutoReset = true;


    // Start the timer

    aTimer.Enabled = true;


    Console.WriteLine("Press the Enter key to exit the program.");

    Console.ReadLine();

}


private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)

{

    Console.WriteLine("The interval has been elapsed");

}


查看完整回答
反對 回復 2021-07-10
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

正如其他人所說, aTimer可能是更好的解決方案。鑒于您對 的使用Stopwatch,您需要將邏輯更改為:


        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        while (true)

        {

            // Check elapsed time w/o stopping/resetting the stopwatch

            // May want to include the 5 seconds themselves (>= instead of >)

            if (stopwatch.Elapsed.Seconds >= 5)

            {

                // At least 5 seconds elapsed, restart stopwatch.

                stopwatch.Stop();

                stopwatch.Start();

                Console.WriteLine("5 s done!");

                // Not sure about this, if you really want to check "periodically",

                // this break makes less sense, because the checking

                // logic will stop after the first 5 seconds have elapsed.

                break;

            }

        }


查看完整回答
反對 回復 2021-07-10
  • 2 回答
  • 0 關注
  • 263 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號