Question

Is there a way in C# where if the time in PC or system says 1:00AM the function will run. I know this can be achieved using timer, but how can I do it? The code I am tinkering right now is this:

var t = new Timer { Enabled = true, Interval = 1 * 1000 };
        t.Tick += delegate { mem_details(); };

But this code runs the function every 1 seconds, Do I need to compute 1:00AM to Seconds so I can do it using this code?

Was it helpful?

Solution 3

Since you're using a 1 second timer why not check the time every tick?

EDIT:

Ok I changed my code a bit. It's still a 1 second timer (1000 milliseconds) but now when the timer tick event triggers, it only looks at the current hour and minute. Thus if your program is running a bit slow it will still run your process at 1 AM.

The global variable "lastRunDate" stores the date of when the last process run. This needs to be updated before your process runs just in case your process takes longer than a second to complete.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private String lastRunDate = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (lastRunDate != System.DateTime.Now.ToString("yyyy-MM-dd"))
            {
                String str = System.DateTime.Now.ToString("h:mm tt");

                if (str.Equals("1:00 AM"))
                {
                    lastRunDate = System.DateTime.Now.ToString("yyyy-MM-dd");
                    MessageBox.Show(str);                    
                }
            }
        }
    }
}

OTHER TIPS

Please have a look to Quartz scheduler, I suppose it will help http://quartz-scheduler.org/

Here is an example of how to configure Quartz for different time using cron strings:http://quartz-scheduler.org/documentation/quartz-2.2.x/examples/Example3.

You can use the online cron generator to select time you need.

You could always go with Microsoft's Reactive Framework to do this. The schedulers in there are incredibly robust.

You could write this code:

var start = DateTimeOffset.Now;
start = start.Date.AddHours(start.Hour == 0 ? 1.0 : 25.0);
/* start is now set for the next 1AM */

/* So schedule it */
Scheduler.Default.Schedule(start, reschedule =>
{
    /* Do you thing as it's now 1AM */

    /* And now reschedule to run tomorrow at 1AM */
    reschedule(DateTimeOffset.Now.Date.AddHours(25.0));
});

You can use timer and check time in it. For example

var t = new Timer { Enabled = true, Interval = 1 * 1000 }; t.Tick += delegate (object sender, EventArgs e) { if (DateTime.Now.TimeOfDay.TotalHours > 1 && DateTime.Now.TimeOfDay.TotalHours < 2) { mem_details(); (sender as Timer).Enabled=false; } };

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top