Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent my console application from closing after a .Net.Timer starts

Tags:

c#

So I have this simple code:

static void Main(string[] args)
  {
      var timer = new Timer(0.5 * 60 * 1000); // 0.5 minutes times 60 seconds times 1000 miliseconds
      timer.Elapsed += new System.Timers.ElapsedEventHandler(Start);
      timer.AutoReset = true;            
      timer.Start();
  }

Which starts a timer that starts the whole program logic. Problem is, the console application closes and nothing is done. How do I prevent this? I know that if I do something like while(true) it works, but that does not seem like an elegant/proper solution.

like image 845
Gaspa79 Avatar asked Dec 09 '22 10:12

Gaspa79


1 Answers

Maybe try

Console.Read(); 

At the end of the code

Thanks to this your console window will not be closed as long as a user will not press a key.

like image 160
Paweł Bejger Avatar answered Dec 11 '22 09:12

Paweł Bejger