c# - How To Periodically Execute A Function -
how can execute c# method every 40 seconds continuously?
you should use timer functionality available in .net
here's code msdn, slight changes.
public class timer1 { private static system.timers.timer atimer; public static void main() { // create timer fourty second interval. atimer = new system.timers.timer(40000); // hook elapsed event timer. atimer.elapsed += new elapsedeventhandler(ontimedevent); // if timer declared in long-running method, use // keepalive prevent garbage collection occurring // before method ends. //gc.keepalive(atimer); } // specify want happen when elapsed event // raised. private static void ontimedevent(object source, elapsedeventargs e) { //code needs repeating every fourty seconds } }
Comments
Post a Comment