简单的说,就是5秒后,触发另个事件
using System;namespace MySpace
{
public class MyClass
{
static void Main()
{
DateTime timer = DateTime.Now;
TimeSpan span = new TimeSpan(0,0,5);
DateTime timer1 = timer+span;
//当前为timer,5秒后为timer1,在timer1触发输出“hello world”事件
//
}
}
}
using System;namespace MySpace
{
public class MyClass
{
static void Main()
{
DateTime timer = DateTime.Now;
TimeSpan span = new TimeSpan(0,0,5);
DateTime timer1 = timer+span;
//当前为timer,5秒后为timer1,在timer1触发输出“hello world”事件
//
}
}
}
1.个人感觉不是很方便(个人问题,见谅)
2.每隔多长时间就触发该事件,如果第一次间隔到了发生了事件后,就停止,怎么控制?
3.下面是 MSDN 的一个小例子,我在OntimedEvent方法中,想加入Timer1.Stop(),可是失败了,怎么办?
using System;
using System.Timers;public class Timer1
{
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine(); GC.KeepAlive(aTimer);
} private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}