Working with a Timer in C#

Implement a simple timer in C#. This excerpt from the upcoming Sams Teach Yourself C# in 21 Days shows you how.

provided by: 
Originally published at Internet.com


This article is an excerpt from the book Sams Teach Yourself the C# Language in 21 Days.

The following listing presents a neat little program that is not well designed. It is simple, and there is nothing complex presented in it. It simply prints the time to the console. Forever.

Listing 1. timer.cs - Displaying the time. 1: // Timer01.cs - Displaying Date and Time 2: // Not a great way to do the time. 3: // Press Ctrl+C to end program. 4: //--------------------- 5: using System; 6: 7: class myApp 8: { 9: public static void Main() 10: { 11: while (true) 12: { 13: Console.Write("r{0}", DateTime.Now); 14: } 15: } 16: }

5/26/2003 9:34:19 PM

As you can see, this listing was executed at 9:34 on May the 26th. This listing presents a clock on the command line. This clock seems to update the time every second. Actually it us updating much more often than that; however, you only notice the changes every second when the value being displayed actually changes. This program runs until you break out of it using Ctrl+C.

In line 13, a call to DateTime is made. DateTime is a structure that is available from the System namespace within the base class libraries. This structure has a static method called Now that returns the current time. There are a large number of additional data members and methods within the DateTime structure. You can check out the .NET Framework class library documentation for information on these. There are also a few other aritcles here on CodeGuru.com ...

Read article at Internet.com site
Regional Articles
- Working with a Timer in C# Alabama
- Working with a Timer in C# Alaska
- Working with a Timer in C# Arizona
- Working with a Timer in C# Arkansas
- Working with a Timer in C# California
- Working with a Timer in C# Colorado
- Working with a Timer in C# Connecticut
- Working with a Timer in C# DC
- Working with a Timer in C# Delaware
- Working with a Timer in C# Florida
- Working with a Timer in C# Georgia
- Working with a Timer in C# Hawaii
- Working with a Timer in C# Idaho
- Working with a Timer in C# Illinois
- Working with a Timer in C# Indiana
- Working with a Timer in C# Iowa
- Working with a Timer in C# Kansas
- Working with a Timer in C# Kentucky
- Working with a Timer in C# Louisiana
- Working with a Timer in C# Maine
- Working with a Timer in C# Maryland
- Working with a Timer in C# Massachusetts
- Working with a Timer in C# Michigan
- Working with a Timer in C# Minnesota
- Working with a Timer in C# Mississippi
- Working with a Timer in C# Missouri
- Working with a Timer in C# Montana
- Working with a Timer in C# Nebraska
- Working with a Timer in C# Nevada
- Working with a Timer in C# New Hampshire
- Working with a Timer in C# New Jersey
- Working with a Timer in C# New Mexico
- Working with a Timer in C# New York
- Working with a Timer in C# North Carolina
- Working with a Timer in C# North Dakota
- Working with a Timer in C# Ohio
- Working with a Timer in C# Oklahoma
- Working with a Timer in C# Oregon
- Working with a Timer in C# Pennsylvania
- Working with a Timer in C# Rhode Island
- Working with a Timer in C# South Carolina
- Working with a Timer in C# South Dakota
- Working with a Timer in C# Tennessee
- Working with a Timer in C# Texas
- Working with a Timer in C# Utah
- Working with a Timer in C# Vermont
- Working with a Timer in C# Virginia
- Working with a Timer in C# Washington
- Working with a Timer in C# West Virginia
- Working with a Timer in C# Wisconsin
- Working with a Timer in C# Wyoming

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML