Chris wrote:
1. What is a difference between System.Windows.Forms.Timer and
System.Threading.Timer ?
The main difference is that the former uses, behind the scenes, the
unmanaged WM_TIMER functionality in Windows, causing timing events to be
raised on the main UI thread. The latter uses the thread pool and
events are raised on a separate thread from the UI thread.
Which one is better depends on what you're doing. Often having the
event raised on the same thread in which you're doing all your other
work is an advantage, but in other scenarios it's a disadvantage.
2. How does it work? How is elpasing time counted? I.e interval is set to
10ms and a timer starts working - elapsing time is being counted. When it
takes 10ms the tick
event is raised. When the new time is being started to count? When the tick
event starts or ends? I mean what is happening when the procedure raised on
the tick event takes longer or shorter time. What will happen if it takes
more time than the interval is set?
As Kevin says, what work the event handler does has very little to do
with accuracy of the interval.
That said, it's important to keep in mind that the Windows thread
scheduling is not perfectly precise. Threads always get to finish their
tiemslice if they want to, and so for short timer intervals that are
roughly the same or shorter than the length of a timeslice in Windows
(generally tens of milliseconds), there can be significant delays beyond
the requested interval.
A 10ms interval, for example, is likely to almost never get scheduled
with any sort of reasonable precision. This may or may not be okay,
depending on why you're using the timer.
Timers of longer duration, hundreds of milliseconds or more, will still
suffer from imprecision, but as a percentage error this will be less.
Pete