Page 1 of 1

Working With Timers Timer and Threaders

#1 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Post icon  Posted 11 March 2008 - 05:56 PM

Ok it seems several people are looking for timer programs. This tutorial will show you how to use Threading and Invoke to update your timer on your program.

Create a new Windows application and add the this using statement
 using System.Threading;


on your form you will need to add a label and call it lblTimerDisplay this will display your timer.

next add a button to start the timer and another button to stop it.

you will need to add a timer delegate and a variable for your delegate here are mine
private delegate void Timer(string time);
Timer timer1;


now declare three variables for the time as a double and one as a string also a boolean for the timer
private double time = 0.0;
        private bool timerStarted = false;
        private string returnTime = string.Empty;


now create a method to update your label with your string value time.
private void UpdateTimer(string time)
        {
            this.lblTimerDisplay.Text = time;
        }



ok now create a property that will return a string value of your timer
public string Counter
        {
            get{return returnTime;}
        }


now create a method that will update your timer in a .01 increment and will invoke your label control.

private void DisplayTimer()
        {
            do
            {
                time += 0.01;
                time = Convert.ToDouble(time.ToString("0.00"));
                returnTime = Convert.ToString(time);
                this.lblTimerDisplay.Invoke(timer1, new object[] { Counter });
            } while (timerStarted);
        }


now you will need to initialize your timer1 delegate to your UpdateTimer method in your constructor
timer1 = new Timer(UpdateTimer);


now on your form double click your start button. In the button click event you will need to set your thread to start the DisplayTimer method you will need to set your timerStarted variable to true and start your thread
private void btnStart_Click(object sender, EventArgs e)
        {
            System.Threading.Thread startTimer = new System.Threading.Thread(
                new System.Threading.ThreadStart(DisplayTimer));

            timerStarted = true;
            startTimer.Start();
        }

now all you have to do is set timerStarted to false in your stop button click event to stop the timer.
private void btnStopTimer_Click(object sender, EventArgs e)
        {
            timerStarted = false;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Working With Timers

#2 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Posted 26 September 2010 - 07:25 PM

for some reason i keep being told that Timer timer1 is having a syntax error, any ideas?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private delegate void Timer(string time);
        Timer timer1;
        private double time = 0.0;
        private bool timeStarted = false;
        private string returnTime = string.Empty;

        private void UpdateTimer(string time)
        {
            this.lblTimerDisplay.Text = time;
        }

        public string Counter
        {
            get { return returnTime; }
        }

        public void DisplayTimer()
        {
            do
            {
                time += 0.01;
                time = Convert.ToDouble(time.ToString("0.00"));
                returnTime = Convert.ToString(time);
                this.lblTimerDisplay.Invoke(timer1, new object[] {Counter});
            }
            while(timeStarted);
        }

        public Form1()
        {
            timer1 = new Timer(UpdateTimer);
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            System.Threading.Thread startTimer = new System.Threading.Thread(
                new System.Threading.ThreadStart(DisplayTimer));

            timeStarted = true;
            startTimer.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timeStarted = false;
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Posted 29 September 2010 - 09:17 AM

I copy/pasted your code and got no error. Can you describe your error further (what line does it occur? What event (like a button click) caused the error?)?
Was This Post Helpful? 0
  • +
  • -

#4 Abdulla Mustafa Qabaha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 10

Posted 05 November 2010 - 04:20 PM

good I use It and work thx

good I use It and work thx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1