Timer Срабатывание события Tick - C#
Формулировка задачи:
Подскажите пожалуйста, каждое следующее событие Tick будет срабатывать с учетом времени работы предыдущего тика или же ровно по времени будет исполняться?
Решение задачи: «Timer Срабатывание события Tick»
textual
Листинг программы
- private static System.Timers.Timer aTimer;
- private static DateTime startTime;
- private static int desiredInterval;
- private static int ticks;
- 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();
- aTimer.Stop();
- aTimer.Dispose();
- Console.WriteLine("Terminating the application...");
- }
- private static void SetTimer()
- {
- // Create a timer with a two second interval.
- startTime = DateTime.Now;
- desiredInterval = 2000;
- aTimer = new System.Timers.Timer(desiredInterval);
- ticks = 0;
- // Hook up the Elapsed event for the timer.
- aTimer.Elapsed += OnTimedEvent;
- aTimer.AutoReset = true;
- aTimer.Enabled = true;
- }
- private static void OnTimedEvent(Object source, ElapsedEventArgs e)
- {
- Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
- e.SignalTime);
- ticks++;
- var desiredTime = startTime.AddMilliseconds((ticks + 1) * desiredInterval);
- var interval = (desiredTime - DateTime.Now).TotalMilliseconds;
- aTimer.Interval = interval;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д