如果我有一個Windows服務需要每30秒執行一次任務,這是更好使用; Timer()類或執行任務的循環會休眠幾秒鐘?Windows服務計劃執行
class MessageReceiver
{
public MessageReceiver()
{
}
public void CommencePolling()
{
while (true)
{
try
{
this.ExecuteTask();
System.Threading.Thread.Sleep(30000);
}
catch (Exception)
{
// log the exception
}
}
}
public void ExecutedTask()
{
// do stuff
}
}
class MessageReceiver
{
public MessageReceiver()
{
}
public void CommencePolling()
{
var timer = new Timer()
{
AutoReset = true,
Interval = 30000,
Enabled = true
};
timer.Elapsed += Timer_Tick;
}
public void Timer_Tick(object sender, ElapsedEventArgs args)
{
try
{
// do stuff
}
catch (Exception)
{
// log the exception
}
}
}
windows服務將創建一個MessageReciever類的實例,並在新線程上執行CommencePolling方法。
看起來像http://stackoverflow.com/questions/1099516/for-a-windows-service - 這是一個更好的等待旋轉或一個定時器,我發現通過搜索「服務計時器」。 – 2009-07-09 07:11:22