I need to make a function that works on it is won thread to do announcements in its determinate time, for example:
- “Visit our website at : ….” set to run every 5 minutes
- “Check your place on the rank at : ….” set to run every 10 minutes
I am a bit confused if I should use a thread timer or a thread while with sleep and how to calculate when every announcements should run.
To get started I was considering the a list of a class to hold each announcement to be made, something like List<MyTimer> with the class as:
public MyTimer
{
public string Announcement { get; set; }
public int Interval { get; set; } // if interval is 0 means
// it will run just once
public bool hasSpecificTime { get; set; } // if this is true then will run
// only once at the SpecificTime
public bool Done { get; set; }
public DateTime SpecificTime { get; set; }
}
Specific time announcements will be removed after used.
As for the thread I was thinking of using something like this:
Timer = new System.Threading.Timer(TimerCallback, null, 0, 1000);
private void TimerCallback(object state)
{
foreach (var item in timerList)
{
// not sure how to handle the intervals of each continuous announcement
// some code here for the the above
// Not sure if this would work either because of the milliseconds etc
// on the DateTime
if (item.hasSpecificTime && !item.Done && SpecificTime == DateTime.Now)
{
SendAnnouncement(item.Announcement);
item.Done = true;
}
}
}
-
Is my implementation OK for what I need ?
-
How do I handle the interval and times within the function as I said in the question I am not sure on how to get it calculated to send it on the correct time ?
You need to change
to
otherwise you will almost all events.
If your
timerListgrow large you should move completed items to a separate list instead to avoid looping through lots of completed items all the time.Edit
You also need to increase
SpecificTimewithIntervalfor the recurring events.You can create more elaborate schemes, but your code is simple to read and understand and that is very important.
One scheme you can use is to store the
MyTimerinstances in an ordered queue where you keep the closestSpecifiedTimefirst. You can then schedule the TimerCallback to the duration until the first element in the queue instead of repeatedly polling a complete list. You need to have some more book-keeping too. If you insert a new item in the queue and that item happens to be the next to be executed (comes first in the queue) you need to cancel and restart your timer.This is more “elegant”, but is only necessary if you have lots of events and suffer from performance issues (or the 1000 ms resolution is not granular enough).