Hi Chris,
1. What is a difference between System.Windows.Forms.Timer and
System.Threading.Timer ?
From the MSDN online documentation:
System.Threading.Timer:
System.Threading.Timer is a simple, lightweight timer that uses callback
methods and is served by thread pool threads. It is not recommended for use
with Windows Forms, because its callbacks do not occur on the user interface
thread. System.Windows.Forms.Timer is a better choice for use with Windows
Forms. For server-based timer functionality, you might consider using
System.Timers.Timer, which raises events and has additional features.
http://msdn2.microsoft.com/en-us/library/system.threading.timer.aspx
System.Windows.Forms.Timer:
A Timer is used to raise an event at user-defined intervals. This Windows
timer is designed for a single-threaded environment where UI threads are
used to perform processing. It requires that the user code have a UI message
pump available and always operate from the same thread, or marshal the call
onto another thread.
When you use this timer, use the Tick event to perform a polling operation
or to display a splash screen for a specified period of time. Whenever the
Enabled property is set to true and the Interval property is greater than
zero, the Tick event is raised at intervals based on the Interval property
setting.
http://msdn2.microsoft.com/en-us/library/system.windows.forms.timer.aspx
System.Timers.Timer:
The Timer component is a server-based timer, which allows you to specify a
recurring interval at which the Elapsed event is raised in your application.
You can then handle this event to provide regular processing. For example,
suppose you have a critical server that must be kept running 24 hours a day,
7 days a week. You could create a service that uses a Timer to periodically
check the server and ensure that the system is up and running. If the system
is not responding, the service could attempt to restart the server or notify
an administrator.
The server-based Timer is designed for use with worker threads in a
multithreaded environment. Server timers can move among threads to handle
the raised Elapsed event, resulting in more accuracy than Windows timers in
raising the event on time. For more information on server-based timers, see
Introduction to Server-Based Timers.
The Timer component raises the Elapsed event, based on the value of the
Interval property. You can handle this event to perform the processing you
need. For example, suppose that you have an online sales application that
continuously posts sales orders to a database. The service that compiles the
instructions for shipping operates on a batch of orders rather than
processing each order individually. You could use a Timer to start the batch
processing every 30 minutes.
http://msdn2.microsoft.com/en-us/library/system.timers.timer.aspx
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?
Aside from directing you back to those 3 references, I can tell you that
*if* the timer is set to fire repeatedly, it will fire at the specified
interval, every 10ms, for example, if you set it that way. The firing of the
event is not related to the interval in any way. As for any event handler
that is attached to the Tick event, it will execute whenever the event
fires, and will not affect the Timer in any way.
--
HTH,
Kevin Spencer
Microsoft MVP
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
news:
[email protected]...