我在 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/