第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

C#
素胚勾勒不出你 2021-07-05 16:53:16
我想在 C# 中定期檢查 60 秒。我可以通過(guò)定期檢查日期來(lái)做到這一點(diǎn),如下所示但是,當(dāng)我使用秒表時(shí),秒表會(huì)重置回開(kāi)始位置,而不是從上次停止時(shí)繼續(xù)。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貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

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


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


例子:


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

}


查看完整回答
反對(duì) 回復(fù) 2021-07-10
?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

正如其他人所說(shuō), aTimer可能是更好的解決方案。鑒于您對(duì) 的使用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;

            }

        }


查看完整回答
反對(duì) 回復(fù) 2021-07-10
  • 2 回答
  • 0 關(guān)注
  • 264 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)