Guapo
Guapo

Reputation: 3482

Send announcements at interval of X or predefined time?

I need to make a function that works on it is won thread to do announcements in its determinate time, for example:

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;
        }
    }
}

Upvotes: 0

Views: 184

Answers (2)

Jsinh
Jsinh

Reputation: 2579

Just a suggestion : Don't get offended please. But in this case you should try Rx operators which will be much cleaner and more maintainable than custom timer or thread implementation. Should thread implementation as much as possible as those are more complex to manage and maintain.

Upvotes: 0

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47058

You need to change

item.hasSpecificTime && !item.Done && SpecificTime == DateTime.Now

to

item.hasSpecificTime && !item.Done && SpecificTime <= DateTime.Now

otherwise you will almost all events.

If your timerList grow 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 SpecificTime with Interval for 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 MyTimer instances in an ordered queue where you keep the closest SpecifiedTime first. 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).

Upvotes: 1

Related Questions