Thread timer

kesdesng

New member
Joined
Jan 12, 2015
Messages
4
Programming Experience
5-10
i need to create a thread timer which works every 1 second asynchronously
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,137
Location
Sydney, Australia
Programming Experience
10+
Are you actually saying that you want to initiate an action on a secondary thread every second? If so then you can use a System.Timers.Timer with its Interval set to 1000. As long as the SynchronizingObject property is set to null, it will raise its Elapsed event on a thread pool thread rather than the UI thread.
 

kesdesng

New member
Joined
Jan 12, 2015
Messages
4
Programming Experience
5-10
yes i am using System.Timers.Timer but i need to use System.Threading Timer because sometimes code line on timer_tick event takes longer than Interval time.
i wil try SynchronizingObject to null hope it works fine thank you.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,137
Location
Sydney, Australia
Programming Experience
10+
yes i am using System.Timers.Timer but i need to use System.Threading Timer because sometimes code line on timer_tick event takes longer than Interval time.
i wil try SynchronizingObject to null hope it works fine thank you.
Um, no. The System.Timers.Timer doesn't even have a Tick event so if your Timer does have a Tick event then it's not a System.Timers.Timer; it's a System.Windows.Forms.Timer. You DO NOT need to use a System.Threading.Timer.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
yes i am using System.Timers.Timer but i need to use System.Threading Timer because sometimes code line on timer_tick event takes longer than Interval time.
If the time needed for the process is longer than the interval, you can stop the timer in the Elapsed event handler, call your process and restart the timer when the process is finished (recalculating the required interval if needed).
 
Top Bottom