Click here to Skip to main content
11,920,701 members (57,870 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked

Pre-beginner's guide to using a timer

, 15 Mar 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
Shows how you can use a timer proc in your windows forms apps


This is my first C# article in a long time. A couple of days ago, Paul Watson was chatting with me on Sonork and he suddenly mentioned that in VB he used to use some function called Now() which used to give him the current date and time instantly. He was complaining that he couldn't find the corresponding .NET function. That's when I did a quick check and discovered the DateTime class or rather structure.

Of course this article is not about the DateTime structure. I suddenly wondered how to use timer based procedures from C#. We all use SetTimer once in a while when we code and it struck me as odd that I never wondered about this in C#. Luckily I had that new Petzold book and Chapter 10 was dedicated to timers and time related functions. Just one page on timers though, though to be fair to him, that was all that was needed. I then played around with it a little.

This article will simply show you how to use them, though if you have the Petzold book that's all you need really. I am also including as an example project, a program that will wait on you and keep popping up a reminder Yes/No message at the time interval you specify, till you click on Yes.

Using Timers

We use the System.Windows.Forms.Timer class for our timer purposes. First we need to create a Timer object. Then we need to set the timer interval. This is accomplished as follows :-

Timer timer01 = new Timer();
timer01.Interval = 1000;

The Timer class has a Tick event. That's exactly what we needed eh? We add our timerproc as our Timer object's event handler as follows :-

timer01.Tick += new EventHandler(timerproc);

Then we simply enable the timer.

timer01.Enabled = true;

That's it. Now every 1000 milliseconds [cause that's the amount we specified], the timerproc gets called.

The timer proc

Within our form class we'll need a function like this :-

void timerproc(object o1, EventArgs e1)
	//blah blah blah
	//blah blah blah
	//blah blah blah

Say, you want to close the timer after some time. Then you can do this :-

((Timer)o1).Tick -= new EventHandler(timerproc);

Before I sign off I better tell you how to get the current time, since that's how this whole article got started Smile | :)


Now people like Paul won't complain that .NET is tougher than VB Wink | ;-)


I am fully aware of the enormously high flame potential this article has and that it is a pre-beginner level thing that I have explained here. But I was quite delighted when I got the timer working and I thought there might be other idiots like me who would be similarly happy. And I simply could not resist the temptation to write another CP article, specially since I have temporarily ceased work on my planned Winsock series.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

QuestionGreat article ! Pin
Wrangly16-Aug-11 0:37
memberWrangly16-Aug-11 0:37 
GeneralMy vote of 5 Pin
M Bester3-Feb-11 0:27
memberM Bester3-Feb-11 0:27 
Easy to understand
QuestionHow to make a timer? Pin
3nity6-Sep-08 8:13
member3nity6-Sep-08 8:13 
Questiontimer won't fire Pin
c-a-b-6-Apr-06 6:31
memberc-a-b-6-Apr-06 6:31 
GeneralTick Event Problem Pin
Anonymous4-Dec-03 18:38
sussAnonymous4-Dec-03 18:38 
GeneralScreenShot Pin
Colin Davies19-Mar-02 19:06
memberColin Davies19-Mar-02 19:06 
GeneralRe: ScreenShot Pin
Nish [BusterBoy]19-Mar-02 19:11
memberNish [BusterBoy]19-Mar-02 19:11 
GeneralRe: ScreenShot Pin
Colin Davies19-Mar-02 19:21
memberColin Davies19-Mar-02 19:21 
GeneralRe: ScreenShot Pin
Nish [BusterBoy]19-Mar-02 19:30
memberNish [BusterBoy]19-Mar-02 19:30 
GeneralRe: ScreenShot Pin
Tim Smith20-Mar-02 3:58
memberTim Smith20-Mar-02 3:58 
GeneralRe: ScreenShot Pin
Nish [BusterBoy]20-Mar-02 4:23
memberNish [BusterBoy]20-Mar-02 4:23 
GeneralAdditionally, Pin
David Wengier18-Mar-02 1:25
memberDavid Wengier18-Mar-02 1:25 
GeneralRe: Additionally, Pin
Nish [BusterBoy]18-Mar-02 1:42
memberNish [BusterBoy]18-Mar-02 1:42 
GeneralWelcome! Pin
Mazdak16-Mar-02 6:15
memberMazdak16-Mar-02 6:15 
GeneralRe: Welcome! Pin
Nish [BusterBoy]17-Mar-02 15:04
memberNish [BusterBoy]17-Mar-02 15:04 
GeneralGood Article Pin
Nick Parker16-Mar-02 2:44
memberNick Parker16-Mar-02 2:44 
GeneralRe: Good Article Pin
Nish [BusterBoy]16-Mar-02 4:12
memberNish [BusterBoy]16-Mar-02 4:12 
GeneralRe: Good Article Pin
Chris Maunder16-Mar-02 9:36
memberChris Maunder16-Mar-02 9:36 
GeneralRe: Good Article Pin
Nish [BusterBoy]17-Mar-02 15:05
memberNish [BusterBoy]17-Mar-02 15:05 
GeneralRe: Good Article Pin
Colin Davies19-Mar-02 19:10
memberColin Davies19-Mar-02 19:10 
GeneralRe: Good Article Pin
Nish [BusterBoy]19-Mar-02 19:14
memberNish [BusterBoy]19-Mar-02 19:14 
GeneralRe: Good Article Pin
Colin Davies19-Mar-02 19:19
memberColin Davies19-Mar-02 19:19 
GeneralRe: Good Article Pin
Nish [BusterBoy]19-Mar-02 19:24
memberNish [BusterBoy]19-Mar-02 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 16 Mar 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid