...Technology Simplified

Thursday, May 24, 2012

Execute a function in C# for every 2 seconds–Windows application

No comments :

I have implemented a sample code to display incremented value of label on button click.
Timer event starts only after button is clicked.
Place label and button on the form and handle button click event.

        private void button1_Click(object sender, EventArgs e)
{
InitTimer();
}
private Timer timer1;
public void InitTimer()
{
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 2000; // in miliseconds
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
TimerCounter();
}
int i = 0;
private void TimerCounter()
{
i = i + 1;
label1.Text = i.ToString();
}


timer

No comments :

Post a Comment