コード例 #1
0
        /// <summary>
        /// Calls the specified callback at the first passing of the h/m/s specified in the scheduledExecutionTime. Once the first
        /// runtime passes, a new timer is created to execute the callback every 24 hours.
        /// </summary>
        private void StartTimers()
        {
            KillTimers();
            int millisecondsToFirstExecution = (int)DayNightManager.GetDateIndependentTimeSpan(DateTime.Now, ScheduledExecutionTime).TotalMilliseconds;

            //If setting the schedule to a time of day that has already passed, calculate the time until its execution the following day
            if (millisecondsToFirstExecution < 0)
            {
                millisecondsToFirstExecution = dayMilliseconds + millisecondsToFirstExecution;
            }
            InitialRunTimer = Constants.CREATE_TIMER(
                () =>
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    Callback();
                    TaskTimer?.Stop();
                    TaskTimer = Constants.CREATE_TIMER(Callback, dayMilliseconds);
                    TaskTimer.Start();
                });
            },
                millisecondsToFirstExecution
                );
            InitialRunTimer.AutoReset = false;
        }