【问题标题】:How can MSDN Timer.Elapsed example work without user interaction?MSDN Timer.Elapsed 示例如何在没有用户交互的情况下工作?
【发布时间】:2014-09-09 15:25:50
【问题描述】:

至少对我来说,这是使用System.Timers.Timer 的完美示例。唯一的问题是,如果我消除Console.ReadLine(),它将无法工作。就我而言,我只想在 5 秒后显示一条消息,然后控制台将关闭。就是这样。

假设我想在没有任何用户交互的情况下显示简单的消息Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime),我该怎么做?换句话说,当我按下 F5 时,我会看到空白的控制台窗口,在 5 秒内我会看到消息,然后控制台就会消失。

这是来自 MSDN 的代码:

using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(5000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program... ");
        Console.ReadLine();
        Console.WriteLine("Terminating the application...");
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}

【问题讨论】:

  • Thread.Sleep 对你来说不够好?
  • @MikeMiller OP already tried 并且有些东西不起作用(不完全清楚是什么)。至少这个问题是直截了当的。

标签: c# .net console-application


【解决方案1】:
using System;
using System.Threading;
using System.Timers;
using Timer = System.Timers.Timer;

private static Timer aTimer;
private static ManualResetEventSlim ev = new ManualResetEventSlim(false);

public static void Main()
{
    // Create a timer with a two second interval.
    aTimer = new System.Timers.Timer(5000);
    // Hook up the Elapsed event for the timer. 
    aTimer.Elapsed += OnTimedEvent;
    aTimer.Enabled = true;
    ev.Wait();
}

private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    ev.Set();
}

【讨论】:

    【解决方案2】:

    有几种不同的方法。

    这是一个例子:

    using System;
    using System.Timers;
    
    public class Example
    {
       private static Timer aTimer;
       private static bool delayComplete = false;
    
       public static void Main()
       {
          // Create a timer with a two second interval.
          aTimer = new System.Timers.Timer(5000);
          // Hook up the Elapsed event for the timer. 
          aTimer.Elapsed += OnTimedEvent;
          aTimer.Enabled = true;
    
          while (!delayComplete)
          {
             System.Threading.Thread.Sleep(100);
          }
       }
    
       private static void OnTimedEvent(Object source, ElapsedEventArgs e)
       {
          Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
          delayComplete = true;
       }
    }
    

    【讨论】:

      【解决方案3】:

      当您的主线程终止时,您的应用程序将退出。那是执行 Main() 的线程。您的计时器将在不同的线程上触发。所以基本上如果你不想做 Console.ReadLine() 你需要做的是 Thread.Sleep(5000) 其中 5000 是线程将休眠的毫秒数。这将使您的主线程等待计时器触发。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-21
        • 1970-01-01
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多