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
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.
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)
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 :-)
Now people like Paul won't complain that .NET is tougher than VB ;-)
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.
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com
where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket
as well as a programming book – Extending MFC applications with the .NET Framework
Nish's latest book C++/CLI in Action
published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.