I recently started to use C# and I wanted to use timers.
I read Windows help about how to declare and define a Timer.
What I don't understand is why I need the Console.ReadLine(); line to start the timer.
(Link to the example)
// Firstly, create a timer object for 5 seconds interval
timer = new System.Timers.Timer();
timer.Interval = 5000;
// Set elapsed event for the timer. This occurs when the interval elapses −
timer.Elapsed += OnTimedEvent;
timer.AutoReset = false;
// Now start the timer.
timer.Enabled = true;
Console.ReadLine(); // <------- !!!
What I want to do but I don't achieve is to start the timer as soon as it is declared. I dont want to write Console.ReadLine(); because I may not need a console.
Example: If i develop a timer class and I call it from an other class, how can I check the timer has been completed?
Thank you in advance.
Console.ReadLineis not there to start the timer but to stop the program from exiting.Console.ReadLine();prevent the application from exiting before the timer expires.