I am working on a Console application that must perform some actions at certain times of the day (I clarify that it is not a service because I need the execution of processes)
The issue is that I must start an application at a certain time and close it in another.
Searching, I found a SO answer in English that does something similar to what I'm looking for but with a single execution, I tried to modify the snippet a bit in the following way:
private void SetUpTimer(TimeSpan alertTime, DateTime current, Action<TimeSpan> delg)
{
TimeSpan timeToGo = alertTime - current.TimeOfDay;
if (timeToGo < TimeSpan.Zero)
{
return;//time already passed
}
this.timer = new System.Threading.Timer(x =>
{
delg(alertTime);
}, null, timeToGo, Timeout.InfiniteTimeSpan);
}
public void OpenProcess(TimeSpan alertTime)
{
Console.WriteLine("Ejecutado {0}",DateTime.Now);
SetUpTimer(alertTime, DateTime.Now.AddDays(1), OpenProcess);
}
and then I call it like this
SetUpTimer(new TimeSpan(13, 44, 00), DateTime.Now, OpenProcess);
The first round is done correctly, but the second time, the OpenProcess () function is not executed
Where is my error?
Thank you!