Well, the idea is to sleep all the way up until the next event, to try to save a lot of extra checks that aren't needed. So the events that only occur like once a week wont need to be taking up anything extra until its their time to run. Instead of checking if its time for guild war every 10 seconds for a whole week, just have a idle thread. Not sure if this is a great idea or not, but this is my implementation of the idea. If you have any thoughts or suggestions please shoot :D. Would also like to hear any of your all's solutions to things like this.
EventPool.cs
IEvent.cs
EventPool.cs
Code:
using System;
using System.Threading;
using System.Collections.Generic;
namespace Base
{
public class EventPool
{
private IEvent[] Events = new IEvent[2];
private byte NextEventToExecute = 0;
public EventPool()
{
Events[0] = new Events.GuildWars_Start();
Events[1] = new Events.GuildWars_Stop();
}
public void Start()
{
for(byte i = 0; i < Events.Length; i++)
if(Events[i] != null)
if (DateTime.Now < Events[i].StartTime && DateTime.Now > Events[i - 1].StartTime)
{ NextEventToExecute = i; break; }
while (true)
{
if (Events[NextEventToExecute] != null)
Events[NextEventToExecute].Execute();
NextEventToExecute++;
if (NextEventToExecute > Events.Length)
NextEventToExecute = 0;
Thread.Sleep((int)(Events[NextEventToExecute].StartTime - DateTime.Now).TotalMilliseconds);
}
}
}
}
Code:
using System;
namespace Base
{
public interface IEvent
{
DateTime StartTime { get; set; }
void Execute();
}
}