I need to create a class object and put it immediately into an array of objects and do it on a timer (every 2 seconds). I wrote a function for the timer and in the final function there should be an action that is repeated, that is, the creation of an object. I don’t know how many objects will end up, but I can stop it at any time
using System;
using System.Timers;
namespace Rectangle
{
class Program
{
private static System.Timers.Timer timer;
private static void SetTimer()
{
//Create timer with two second interval
timer = new System.Timers.Timer(200);
//Hook up the Elapsed event for timer
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
}
public static void Main()
{
SetTimer();
Console.WriteLine("\nPress the Enter key to exit the application...\n");
Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
Console.ReadLine();
timer.Stop();
timer.Dispose();
Console.WriteLine("Terminating the application...");
Console.WriteLine("Size: {0}", Rectangle.size);
Console.ReadKey();
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
}
}
}