The System.Windows.Forms.Timer
timer uses the message pump of the UI to marshal the tick event, a service doesn't run a message pump by default so without a little extra work the System.Windows.Forms.Timer
timers will not work.
The System.Timers.Timer
is a server-based timer and raises an event on the thread you create it on (I think). If this isn't working, perhaps you aren't starting the timer or the timer runs on a thread that immediately ends (as in, nothing is keeping the thread alive so it finishes).
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
The System.Threading.Timer
timer uses a callback that runs on a ThreadPool
thread and isn't tied to the message pump at all, hence this worked.
When you run Application.Run(myForm)
in a WinForms project, that call also runs up the message pump, this manages UI messages. The windows timer you mention is a UI component and expects the message pump to be running in order to cause the tick event to occur on the UI thread.
Take a look here for running a message pump in a Windows Service:
Message pump in .NET Windows service
Further reading:
http://support.microsoft.com/kb/842793
In conclusion, I'd just go with the System.Threading.Timer
class.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…