C#每隔一段时间执行一次函数,需要用到C# Timer对象,例如小编使用Timer对象实现每隔1秒钟更新一次时间,方法如下:using System;
using System.Timers;
namespace commandDemo
{
class Program
{
static void Main(string[] args)
{
Timer timer = new Timer(1000);
timer.AutoReset = true;
timer.Elapsed += HandleTimer;
timer.Start();
Console.ReadLine();
}
private static void HandleTimer(Object obj, ElapsedEventArgs e)
{
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(time);
}
}
}
HandleTimer是自定义的函数,用于输出yyyy-MM-dd HH:mm:ss格式的时间,这是一个同步每隔一秒更新一次时间的方法。
来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/kuozhan/494.html