我正在寻找一些关于使我的代码更高效的建议。我想做的是System.Threading.Timer
每隔一小时左右运行一些工作,工作不会很繁重,但我希望有一个不占用太多资源的代码。我计划在 Windows 服务中运行此代码。
这就是我到目前为止所拥有的。
class Program
{
private static Timer timer;
static void Main(string[] args)
{
SetTimer();
}
static void SetTimer()
{
timer = new Timer(Write);
var next = DateTime.Now.AddHours(1);
var nextSync = (int)(next - DateTime.Now).TotalMilliseconds;
timer.Change(nextSync, Timeout.Infinite);
}
static void Write(object data)
{
Console.WriteLine("foo");
SetTimer(); //Call the SetTimer again for the next run.
}
}
你们有什么感想?我可以让我的代码更有效率吗?
非常感谢所有建议!