c#使用timer定时器

private static Timer aTimer;
public void Index()
{
      aTimer = new Timer(1000);
      aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
      aTimer.AutoReset = true;
      aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
      System.Diagnostics.Debug.WriteLine("触发的事件发生在: {0}", e.SignalTime);
}

然后调用index方法即可。

参考:https://www.cnblogs.com/hdkn235/p/4187925.html

官方文档:https://docs.microsoft.com/zh-cn/dotnet/api/system.timers?redirectedfrom=MSDN&view=net-5.0

posted @ 2021-07-19 17:01  masha2017  阅读(116)  评论(0编辑  收藏  举报