aparna km

aparna km

  • NA
  • 2
  • 1.8k

How to call items at specific time in timers

Sep 11 2014 3:09 AM
I have to start timer at 12AM daily. It has to execute 4 items i have with 15min interval
i.e, item1 should be executed @12, item2 @12 15 and so on
i have written below code:
To start timer @12Am :-
private void SetTimerValue()
{
//// trigger the event at 12 AM.
DateTime requiredTime = DateTime.Today.AddHours(0).AddMinutes(00);
if (DateTime.Now > requiredTime)
{
requiredTime = requiredTime.AddDays(1);
}
TimeSpan periodTS = requiredTime - DateTime.Now;
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction), null, 0, (long)periodTS.TotalMilliseconds);
}
In TimerAction i have 4 items which has to be called @ 15min interval
public void TimerAction(object e)
{
var collection = new List<string> { "item1", "item2", "item3", "item4" };
// When timer is 12am, execute item1
// When timer is 12 15am, execute item2
// When timer is 12 30am, execute item3
// When timer is 12 45am, execute item4
}
My question is :
1. Can i create seperate timer inside TimerAction method?
2. How should i loop and execute all these 4 items?
3. Is there any better approach?. Please guide me to do this.