c# - 在 C# 中应用程序结束之前计时器不会触发

标签 c# multithreading timer

我在 C# 中有一个简单的控制台应用程序,它创建一个计时器来将文本写入控制台。然后它等待用户按下一个键。

如果用户在五秒内按下某个键,则应用程序结束,并且计时器永远不会触发。如果用户没有按下任何键,则计时器会按预期触发。

为什么计时器创建的线程不能阻止应用程序终止?如何确保即使用户按下某个键,应用程序也能继续运行?

static void Main(string[] args)
    {
        System.Timers.Timer timer = new System.Timers.Timer(5000);
        timer.Interval = 5000; // 5 seconds
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;
        timer.AutoReset = true;
        timer.Start();
        Console.ReadKey();
    }

    public static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("Timer Fired.");
    }

最佳答案

要让计时器消息始终写出,您可以让主线程等待reset event。一旦计时器被触发,就会发出信号。

    static ManualResetEvent timerFired = new ManualResetEvent(false);

    static void Main(string[] args)
    {
        System.Timers.Timer timer = new System.Timers.Timer(5000);
        timer.Interval = 5000; // 5 seconds
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;
        timer.AutoReset = true;
        timer.Start();
        Console.ReadKey();

        // This call will return only after timerFired.Set() is
        // called.  So, if the user hits a key before the timer is
        // fired, this will block for a little bit, until the timer fires.  
        // Otherwise, it will return immediately
        timerFired.WaitOne();
    }

    public static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("Timer Fired.");
        timerFired.Set();
    }

至于另一个问题,为什么不阻止应用程序退出,我来试一试。 timer_Elapsed() 正在从后台线程调用。根据 MSDN,后台线程不会保持执行环境的运行。有一个 nice discussion在这里。

ThreadPool 线程是后台线程,MSDN Timer 文档表明 Timer 的 Elapsed event在 ThreadPool 线程上引发,因此应用程序不会等待它,因为它不是前台线程。

关于c# - 在 C# 中应用程序结束之前计时器不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958759/

相关文章:

Linux 内核 : Why add_timer() is modifying my "expires" value?

c# - .NET 图像库

c# - 获取对类的引用并仅从字符串值执行静态方法?

python - BeautifulSoup无法在Python的多线程程序中工作

java - ArrayList vs HashMap——大量的迭代和对象操作

在多线程编程中更改值

jquery - 在哪里可以下载 jQuery 计时器?

command-line - jmeter 从命令行以无暂停模式启动

c# - 闪烁游戏对象

c# - 将 ListView 列值与另一个 ListView 列值进行比较,如果匹配则显示列值而不是其他列值