[Solved] Call method at set time when program is already running?


Use a System.Threading.Timers.Timer object;

Run the timer starting the next instance of your desired time and every 24 thereafter.

// TimerCallback object that wraps your method to call when timer elapses 
// (see end of answer)
var tc = new TimerCallback(DoStuffAt1300Hours);

// Tomorrow 2/25/14 at 1pm
var dt = new DateTime(2014, 2, 25, 13, 0, 0);

// dt - DateTime.Now = start timer in whatever time is between now and tomorrow at 1pm
// This is a delay, so effectively, the timer will start tomorrow at 1pm
// And it will run every 24 hours, calling the method DoStuffAt1300Hours()
var timer = new Timer(tc, null, dt - DateTime.Now, TimeSpan.FromHours(24));

Create a callback that will execute what you want;

public static void DoStuffAt1300Hours(object o)
{
    Console.WriteLine("Hey it's 1pm!");
}

0

solved Call method at set time when program is already running?