locked
timer RRS feed

  • Question

  • hi i would love to create a timer in which like the timer runs a function wait 5 seconds and run another, like every 5 second it runs a function how could i do that in c# 
    Programming Best of Best
    Tuesday, August 19, 2008 1:15 AM

Answers

  • Create a Timer in Sysyem.Timers Namespace.

    http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx


    //Create a timer with a five second interval.     
    aTimer = New System.Timers.Timer(5000)   
     

    Gaurav Khanna
    Tuesday, August 19, 2008 4:03 AM
  • To create the actual timer,  the previous example is perfect.  ( the 5000 represents the interval in milliseconds )

    But, to make it actually do something every 5000 milliseconds (5 seconds), you have to add the following :


    aTimer.Tick += new System.EventHandler(TimerTick); 
     
    public void TimerTick(object sender, EventArgs e) 
       //Put the code to be executed here 



    Give'r
    Tuesday, August 19, 2008 4:32 AM
  • Note that the System.Timers Timer raises the Elapsed event on a ThreadPool thread and if you want to manipulate GUI controls and component you should use the Timer's SynchronizingObject to synchronize the event with the GUI thread. Another option in this case is to use a Windows.Forms timer.


    /Ruben RJJournal
    Tuesday, August 19, 2008 5:33 AM

All replies

  • Create a Timer in Sysyem.Timers Namespace.

    http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx


    //Create a timer with a five second interval.     
    aTimer = New System.Timers.Timer(5000)   
     

    Gaurav Khanna
    Tuesday, August 19, 2008 4:03 AM
  • To create the actual timer,  the previous example is perfect.  ( the 5000 represents the interval in milliseconds )

    But, to make it actually do something every 5000 milliseconds (5 seconds), you have to add the following :


    aTimer.Tick += new System.EventHandler(TimerTick); 
     
    public void TimerTick(object sender, EventArgs e) 
       //Put the code to be executed here 



    Give'r
    Tuesday, August 19, 2008 4:32 AM
  • Note that the System.Timers Timer raises the Elapsed event on a ThreadPool thread and if you want to manipulate GUI controls and component you should use the Timer's SynchronizingObject to synchronize the event with the GUI thread. Another option in this case is to use a Windows.Forms timer.


    /Ruben RJJournal
    Tuesday, August 19, 2008 5:33 AM
  • Hi

    I have a WPF app with a single thread, it has a timer and some controls to paint the screen. 

    the timer's callback function does not allow running any function of the main thread.

    the Windows.Form.Timer does not exist.

    how do I synchronize the timed out event with the main thread functions?

    thank you

     

    Tuesday, January 3, 2012 10:28 PM
  • For anyone who has the problem with STA running in another thread when using the "System.Timers.Timer ",

    you should use this  "System.Windows.Threading.DispatcherTimer" instead.

    more info here:

    http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

    Tuesday, January 3, 2012 11:20 PM