I\'m working on a program that will need to delete a folder (and then re-instantiate it) at a certain hour of the day, and this hour will be given by the user.
The h
If you want to do this in your code you need to use the Timer class and trigger the Elapsed event.
A. Calculate the time left until your first runtime.
TimeSpan day = new TimeSpan(24, 00, 00); // 24 hours in a day. TimeSpan now = TimeSpan.Parse(DateTime.Now.ToString("HH:mm")); // The current time in 24 hour format TimeSpan activationTime = new TimeSpan(4,0,0); // 4 AM TimeSpan timeLeftUntilFirstRun = ((day - now) + activationTime); if(timeLeftUntilFirstRun.TotalHours > 24) timeLeftUntilFirstRun -= new TimeSpan(24,0,0); // Deducts a day from the schedule so it will run today.
B. Setup the timer event.
Timer execute = new Timer(); execute.Interval = timeLeftUntilFirstRun.TotalMilliseconds; execute.Elapsed += ElapsedEventHandler(doStuff); // Event to do your tasks. execute.Start();
C. Setup the method do execute what you want to do.
public void doStuff(object sender, ElapsedEventArgs e) { // Do your stuff and recalculate the timer interval and reset the Timer. }