MorkaLork Development

Interesting stuff I've picked up over the years...

C# Timer

2009-05-30 13:32:48 | 2213 views | C# timer control tick _tick enabled

About the timer class?



From MSDN:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.


The timer is a useful class that can be used to measure time elapsed between two events. The timer can be started by setting the .Enabled property to true or by using the Start() method. Likewise it can also be turned off by setting the .Enabled property to false or by using the Stop() method.

An example:



Let's make a small forms application that displays a message every second (every 1000 ms that is). We add two buttons (btnStart, btnStop) and a textbox (txtMain) to a form. After that we add a timer control (timTimer) by dragging the timer control onto the form. The timer control will then be represented by a small icon below, it will look like this:

A timer control dragged onto a form

After that we alter the properties of the control and set the .Interval property to 1000 ms:

Set the timer Interval property to 1000

Then we take a look at the eventlist (by pressing the yellow flash above the properties) and we can see that there is only one event, Tick. Double click on it to open it up for editing:

The timer eventlist

We can now add something to the event, and we will. This is what we'll do:



private void timTimer_Tick(object sender, EventArgs e)
{
txtMain.AppendText(string.Format("Time: {0}\n", DateTime.Now.ToLongTimeString()));
}


This means that for every tick (for every 1000ms/1sec) the event will fire and add "Time: xxx" to the txtMain textbox.

But we need to be able to fire the event as well, so we'll add something to the buttons click event as well:



private void btnStart_Click(object sender, EventArgs e)
{
timTimer.Enabled = true;
}

private void btnStop_Click(object sender, EventArgs e)
{
timTimer.Enabled = false;
}


The first button, btnStart, will enable the tick, and while enable it will every second enter a new line into the textbox. The second button, btnStop, will stop the timer and it's Tick event.

This is what it will look like:

A working application using the timer control


The full code:





public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void timTimer_Tick(object sender, EventArgs e)
{
txtMain.AppendText(string.Format("Time: {0}\n", DateTime.Now.ToLongTimeString()));
}

private void btnStart_Click(object sender, EventArgs e)
{
timTimer.Enabled = true;
}

private void btnStop_Click(object sender, EventArgs e)
{
timTimer.Enabled = false;
}
}



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.