C # Run a function at a specific time

I made a small Windows Forms program to do some automatic backup of some files on my disk. Right now I need to press a button before it is executed, but I want the program to work and start at 3 o’clock the function that performs the backup.

I did a little research and found out that I needed a timer for this, but I could not use it so that it would run at a specific time.

I hope you can help me. :-)

Thanks!

+5
source share
6 answers

Not 100% cheating, but you will find this CO question interesting.;)

quartzTimer .

+4

: System.Forms.Timer

, , :

DateTime TimeToExecuteTask
DateTime Now = DateTime.Now // assign values.

int SecondsToExectution = (TimeSpan)(TimeToExecuteTask - Now).TotalSeconds;
+2

- . , .

, (, )

myTimer.Interval = 1000;

, , .

+2

Windows . . Windows, , .

, AT .

, ( , ), - AT , . , . , , , , .

+2
source

sorry for posting an answer after so many years. I feel that my post can help someone later. here is a small code to simulate a schedule task with a timer.

using System;
using System.Timers;

namespace ScheduleTimer
{
    class Program
    {
        static Timer timer;

        static void Main(string[] args)
        {
            schedule_Timer();
            Console.ReadLine();
        }

        static void schedule_Timer()
        {
            Console.WriteLine("### Timer Started ###");

            DateTime nowTime = DateTime.Now;
            DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 8, 42, 0, 0); //Specify your scheduled time HH,MM,SS [8am and 42 minutes]
            if (nowTime > scheduledTime)
            {
                scheduledTime = scheduledTime.AddDays(1);
            }

            double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;
            timer = new Timer(tickTime);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("### Timer Stopped ### \n");
            timer.Stop();
            Console.WriteLine("### Scheduled Task Started ### \n\n");
            Console.WriteLine("Hello World!!! - Performing scheduled task\n");
            Console.WriteLine("### Task Finished ### \n\n");
            schedule_Timer();
        }
    }
}

Screenshot

enter image description here

0
source
    //Perhaps the following is a more suitable solution consume the following

    public class DailyTrigger
    {  
        readonly TimeSpan triggerHour;

        public DailyTrigger(int hour, int minute = 0, int second = 0)
        {
          triggerHour = new TimeSpan(hour, minute, second);
          InitiateAsync();
        }

        async void InitiateAsync()
        {
          while (true)
          {
            var triggerTime = DateTime.Today + triggerHour - DateTime.Now;
            if (triggerTime < TimeSpan.Zero)
              triggerTime = triggerTime.Add(new TimeSpan(24, 0, 0));
            await Task.Delay(triggerTime);
            OnTimeTriggered?.Invoke();
          }
        }

        public event Action OnTimeTriggered;
      }


   // as Follows

     var trigger = new DailyTrigger(17); // every day at 5:00pm

          trigger.OnTimeTriggered += () =>
          {
            //Do Logic
          };
0
source

All Articles