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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -