How does timer work?

G

Guest

1. What is a difference between System.Windows.Forms.Timer and
System.Threading.Timer ?
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?
 
K

Kevin Spencer

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]...
 
P

Peter Duniho

Chris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top