问题描述
在我的应用程序必须定期心跳发送到兄弟的应用程序。
这是较好地完成与System.Timers.Timer / Threading.Timer或使用线程while循环和Thread.sleep代码?
心跳间隔为1秒。
而(!出口)
{
//做工作
Thread.sleep代码(1000);
}
或
myTimer.Start(()=> {
//做工作
},1000); //伪code(而不是实际的语法)...
System.Threading.Timer有我的投票。
System.Timers.Timer是为基于服务器的使用(你code运行为主机上的服务器/服务,而不是被一个用户运行)的定时器功能。
while循环和Thread.sleep代码指令螺纹是真正赋予更强大的定时器mecahnisms在.NET中是否存在等一个坏主意。
In my application I have to send periodic heartbeats to a "brother" application.
Is this better accomplished with System.Timers.Timer/Threading.Timer or Using a Thread with a while loop and a Thread.Sleep?
The heartbeat interval is 1 second.
while(!exit)
{
//do work
Thread.Sleep(1000);
}
or
myTimer.Start( () => {
//do work
}, 1000); //pseudo code (not actual syntax)...
System.Threading.Timer has my vote.
System.Timers.Timer is meant for use in server-based (your code is running as a server/service on a host machine rather than being run by a user) timer functionality.
A Thread with a While loop and Thread.Sleep command is truly a bad idea given the existance of more robust Timer mecahnisms in .NET.
这篇关于System.Timers.Timer / Threading.Timer VS与while循环+的Thread.Sleep主题周期性任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!