System.Timer.Timer先执行后等待

hyunwung hyunwung 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
System.Timer.Timer控件的问题。

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);

尝试了一下,timer是在线等待2秒之后,去执行的回调函数。启动->(2秒后)->执行OnTimedEvent函数->然后每隔两秒执行一次。

想问的是,可不可以让timer启动就去执行回调函数,然后再开始重复。就是:启动->执行OnTimedEvent函数->(2秒后)->然后每隔两秒执行一次。
...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
hyunwung
C# 111,128 成员 642,536 内容