What i have got at the moment is a timer that fires every 5000 ms:
static Timer _aTimer = new System.Timers.Timer();
static void Main(string[] args)
{
_aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_aTimer.Interval = 5000;
_aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
On fire it then sets up the queues for processing the files:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// stop the timer so we dont reprocess files we already have in the queue
StopTimer();
// setup a list of queues
var lists = new List<IncomingOrderQueue>();
//get the accounts in which the files we are looking in
var accounts = new List<string>() { "Account1", "Account2" };
//loop through the accounts and set up the queue
foreach (var acc in accounts)
{
// create the queue
var tmp = new IncomingOrderQueue();
// for each file in the folders add it to be processed in the queue
foreach (var orderFile in OrderFiles(acc))
{
tmp.EnqueueSweep(new QueueVariables() { Account = acc, File = orderFile });
}
// add the queue to the list of queues
lists.Add(tmp);
}
// for each of the queues consume all the contents of them
Parallel.ForEach(lists, l => l.Consume());
//start the timer back up again because we have finished all the files we have in the current queue
StartTimer();
}
public static void StopTimer()
{
Console.WriteLine("Stop Timer");
_aTimer.Stop();
_aTimer.Enabled = false;
}
public static void StartTimer()
{
Console.WriteLine("Start Timer");
_aTimer.Enabled = true;
_aTimer.Start();
}
The Blocking Queue its self:
public class IncomingOrderQueue
{
BlockingCollection<QueueVariables> _orderQ = new BlockingCollection<QueueVariables>();
public void EnqueueSweep(QueueVariables incoming)
{
// add items to the queue
_orderQ.Add(incoming);
}
public void Consume()
{
// stop anything been adding to the queue
_orderQ.CompleteAdding();
// consume all the objects in the blocking collection
Parallel.ForEach(_orderQ.GetConsumingEnumerable(), Processor.Order.Object);
}
public int QueueCount
{
get
{
return _orderQ.Count;
}
}
}
What i have works how it should, start the timer -> stop the timer -> trigger the process for collecting all the files within the folders -> process all the files -> restart the timer.
I cant help but think there is a better way to do what im doing especially when the number of queues that are going to be created for the accounts is 200 - 400.
Thanks