You are here:About>Computing & Technology>Visual Basic> Using VB.NET> Of Ticks and Timers
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

Of Ticks and Timers

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Dec 22 2007

There's a lot of "time related" stuff in .NET

Ticks and Timers in .NET start with the Tick. The Tick is the smallest time unit in .NET and is equal to 100 nanoseconds. (We'll see a code implication of that soon.)

The use of Ticks and Timers in .NET is one of those musty corners that people use, but don't always understand. For example, in researching this article, I came across this statement in another web page:

.Net provides a special property used to count a specific number of lapses that have occurred since you started your computer.

The question occured to me, what is a "lapse"? We're going to be much more specific here. It's more correct to say, that the TickCount property of the Environment object is the number of milliseconds since you started your computer.

When you add a Timer object to your .NET program, it simply converts the interval to Ticks to determine when to trigger the Tick event. Yes, Virginia, Tick is an event but Ticks is a property. Ticks (a property of the Date datatype) and TickCount (a property of .NET's new Environment object) are not the same.

Ticks (note the plural) equals the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight on January 1, 0001. (It's a testament to the power of 64 bit integers to represent large numbers.) This code shows them both together:

Debug.Print("TickCount: " & _
   Environment.TickCount.ToString)
Debug.Print("Ticks: " & _
   Now.Ticks.ToString)

TickCount: 31859593
Ticks: 633339399613462740

Although Ticks is an Int64, TickCount is only an Int32. Consequently, TickCount will "roll over" every 49.8 days. If you're writing a system that runs continuously, this can (and has) caused some bugs. With an eye to the future, Microsoft helpfully provides the minimum and maximum values that Ticks and TickCount can have. (No more "millenium bugs"!) These are constant values now, but they could change in some distant future compiler. This is what you get today:

Debug.Print("Max Value - Ticks: " & _
   DateTime.MaxValue.Ticks.ToString)
Debug.Print("Max Value - TickCount: " & _
   Environment.TickCount.MaxValue.ToString)

Max Value - Ticks: 3155378975999999999
Max Value - TickCount: 2147483647

This means we have 2522039563641444794 Ticks to go before we "run out of gas". I think I'll worry about something else.

That's what they are. Now what do you do with them. See that on the next page.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.