I tried to generalize the delayed action call proposed in Delayed function calls To use it for scheduling tasks that I need executed in the next 24h, but it does not work. I don't understand why the direct call proposed there works while my generalized one does not do anything.
public void RunScheduledTasks()
{
//This generic way fails
RunScheduledMethod(bar1, new TimeSpan(15, 31, 30)); //DOES NOTHING
//This way works
TimeSpan Time1 = new TimeSpan(15, 31, 30);
if (Time1 > DateTime.Now.TimeOfDay)
{
Time1 -= DateTime.Now.TimeOfDay;
Task.Delay(Time1).ContinueWith(t => bar1()); //WORKS OK.
}
}
public void RunScheduledMethod(Action methodToRun, TimeSpan TimeToRun)
{
TimeToRun -= DateTime.Now.TimeOfDay;
Task.Delay(TimeToRun).ContinueWith(t => methodToRun);
}
public void bar1()
{
MessageBox.Show("Bar1 called" + DateTime.Now.TimeOfDay);
}