System.Timers.Timer 奇怪问题
using System;using System.Timers;
public class Timer1
{
public static void Main()
{
// Normally, the timer is declared at the class level, so
// that it doesn't go out of scope when the method ends.
// In this example, the timer is needed only while Main
// is executing. However, KeepAlive must be used at the
// end of Main, to prevent the JIT compiler from allowing
// aggressive garbage collection to occur before Main
// ends.
System.Timers.Timer aTimer = new System.Timers.Timer();
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
测试了这msdn官方的demo 发现一段时间后会自动停止,不知道怎么回事,请各位大大指教! --------------------编程问答-------------------- 不会自动停止啊 只是在你敲了回车之后Console.ReadLine();执行完了控制台就关闭了程序结束了 --------------------编程问答-------------------- Console.ReadLine();会挂起。不知道你说的是不是这个。 --------------------编程问答-------------------- 你没有做任何操作?诡异 应该是要读一个字符的。。。。。。
补充:.NET技术 , C#