How to use timer control in C#?

Discussion in 'C#' started by shah123, May 2, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    How to use timer control in C#?

    Any example please? Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    1. Have the timer control in your application or have a variable of type Timer.
    2. Assign a event to the Timer tick event.
    3. Enable the timer and start when you think you need the event.
    4. After sufficient time elapse your function will be called.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    I managed before you said above but thanks Shabbir. I did above now how to show current system time on the form at some specific position. Following is some sample of code I have
    Code:
    public Form1()
            {
                InitializeComponent();
    
                // Instantiate the timer
                timer1 = new Timer();
    
                // Setup timer
                timer1.Interval = 1000; //1000ms = 1sec
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Start();
              
            }
    
    Code:
     private void timer1_Tick(object sender, EventArgs e)
            {
                if (sender == timer1)
                {
                    DateTime.Now.ToLongDateString();
                }
            }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice