【问题标题】:Periodically checking for 5 seconds using Stopwatch in C#在 C# 中使用秒表定期检查 5 秒
【发布时间】:2018-05-29 05:01:23
【问题描述】:

我想在 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();
        }
    }
}

【问题讨论】:

  • 您无需停止秒表即可读取其当前值。
  • 另外,这看起来像是 Timer 的工作,而不是 StopWatchDateTime
  • 请写清楚您的要求。你想每隔 60 多岁就重复做一项任务吗?
  • 请写清楚您的要求。我们无法理解您的问题。
  • 您正在运行一个非常频繁地运行的循环并检查已用时间。你最好使用Thread.Sleep(5000) 而不是你所有的代码。

标签: c# stopwatch


【解决方案1】:

尝试使用计时器 (System.Timers) 而不是秒表。 设置所需的时间间隔并对 Elapsed 事件执行必要的操作。

Here你可以了解更多。

例子:

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

【讨论】:

    【解决方案2】:

    正如其他人所说,Timer 可能是更好的解决方案。鉴于您对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;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 2019-09-05
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多