Trouble using timer class in C# -
basically i'm trying implement timer in class would-be first game in c#; want use timers update player , provide periodic feedback, can't seem 'timer' class work correctly. if use in loop (as shown below), wait 2 seconds, , keep writing "you're alive!" console 0 delay in-between; , if not use loop, application ends instantly.
using system; using system.timers; public class myclass { public static void mytimer_elapsed(object sender, elapsedeventargs e) { console.writeline("you're alive!"); } public static void main(string[] args) { while (true) { timer mytimer = new timer(); mytimer.interval = 2000; mytimer.enabled = true; mytimer.elapsed += new elapsedeventhandler(mytimer_elapsed); mytimer.start(); } }
just add below line after mytimer.start();
console.readline();
static void main(string[] args) { timer mytimer = new timer(); mytimer.interval = 2000; mytimer.enabled = true; mytimer.elapsed += new elapsedeventhandler(mytimer_elapsed); mytimer.start(); console.readline(); } private static void mytimer_elapsed(object sender, elapsedeventargs e) { console.writeline("you're alive!"); }
Comments
Post a Comment