C# DateTime简单的定时器用法

DateTime是C#中的时间类,有公共索引器Now可以获取当前时间。

如果制作简单定时器的话,可以这样:

初始化:DateTime lasttime=DateTime.Now;

           TimeSpan timeinterval=new TimeSpan(1000);

bool CheckTimer()

{

  if(DateTime.Now.Substract(lasttime)>timeinterval) return true;

  return false;

}

TimeSpan是时间间隔类,专用做两个时间之差。另外DateTime包含丰富的时间格式,有需要的可以看看~

原文地址:https://www.cnblogs.com/xhyu/p/4492617.html