473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How does timer work?

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?
Oct 1 '07 #1
1 2352
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
Oct 1 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Besta | last post by:
Hello all, I am having trouble creating a windows service with a timer. Everything seems to go ok but the elapsed event does not fire.Can anyone shed any light on this, may be something simple as...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
21
by: Willie jan | last post by:
place this behind a button that fills a listbox. as you will see the time is now and then 0 or filled in???????????? by hitting the button. is there a way to determine the real elapsed time? ...
11
by: Philip Wagenaar | last post by:
Hello, I am using a timer object in my Windows Forms Application. Does the code in ..elapsed event run in a diffrent thread? If the interval is set to 10 milliseconds and the time to execute the...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
9
by: archana | last post by:
Hi all, I want to know about interval of timer. I am using timer in windows service.I head somewhere that when i set interval property of timer while setting interval, restart time of Pc is...
10
by: Mythmon | last post by:
I am trying to make a program that will basically simulate a chess clock in python. To do this I have two threads running, one that updates the currently running clock, and one that watches for a...
8
by: KnighT | last post by:
I have a .net service that runs a System.Threading.Timer. The delegate points to the function that the service should execute when the timer elapses. Problem: The timer is not ticking. I have...
8
by: Jerry Spence1 | last post by:
Do Timers operate on the UI thread, or do they have it's own thread? I'm trying to assess the pros and cons for using a timer or a thread for a certain application. -Jerry
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.