public void AddEvent(ITimestampedEventQueueItem item)
        {
            //find the index by doing a simple linear search

            //the earliest items are at the top of this queue and the latest items on the bottom.
            //the goal is to start from the bottom and insert right after the first item that is
            //before us.

            if (length >= events.Length)
            {
                Console.WriteLine("Circular queue is full.");
                return;
            }

            //add the item
            events[tail] = item;
            if (tail == events.Length - 1)
            {
                tail = 0;
            }
            else
            {
                tail++;
            }
            length++;

            //else
            //{
            //    //validate the measurement...
            //    //if (Math.Abs(events[events.Count - 1].TimeStamp - item.TimeStamp) > maxAgeDifference)
            //    //{
            //    //    Console.WriteLine("WARNING: ERRONEOUS MEASUREMENT TIMESTAMP!!! Last Measurement TS: " + events[events.Count - 1].TimeStamp + " Recieved: " + item.TimeStamp);
            //    //    events.Clear();
            //    //    return;
            //    //}

            //    for (int i = events.Count - 1; i >= 0; i--)
            //    {
            //        if (item.CompareTo(events[index]) < 0) //item is lt events[index]
            //        {
            //            index = i ;
            //            break;
            //        }
            //    }
            //    events.Insert(index, item);
            //}
        }
        public void AddEvent(ITimestampedEventQueueItem item)
        {
            //find the index by doing a simple linear search

            //the earliest items are at the top of this queue and the latest items on the bottom.
            //the goal is to start from the bottom and insert right after the first item that is
            //before us.

            if (length >= events.Length)
            {
                Console.WriteLine("Circular queue is full.");
                return;
            }

            //add the item
            events[tail] = item;
            if (tail == events.Length - 1) tail = 0;
            else tail++;
            length++;

            //else
            //{
            //    //validate the measurement...
            //    //if (Math.Abs(events[events.Count - 1].TimeStamp - item.TimeStamp) > maxAgeDifference)
            //    //{
            //    //    Console.WriteLine("WARNING: ERRONEOUS MEASUREMENT TIMESTAMP!!! Last Measurement TS: " + events[events.Count - 1].TimeStamp + " Recieved: " + item.TimeStamp);
            //    //    events.Clear();
            //    //    return;
            //    //}

            //    for (int i = events.Count - 1; i >= 0; i--)
            //    {
            //        if (item.CompareTo(events[index]) < 0) //item is lt events[index]
            //        {
            //            index = i ;
            //            break;
            //        }
            //    }
            //    events.Insert(index, item);
            //}
        }
        public void AddEvent(ITimestampedEventQueueItem item)
        {
            //find the index by doing a simple linear search

            lock (this)
            {
                //the earliest items are at the top of this queue and the latest items on the bottom.
                //the goal is to start from the bottom and insert right after the first item that is
                //before us.
                if (events.Count >= capacity)
                {
                    //if (!emptyWithoutError)
                        //Console.WriteLine("EVENT QUEUE IS FULL. OLDEST MEASUREMENT PURGED.");
                    Pop();
                }

                //Chuck measurements that are too old
                if ((curTS - item.TimeStamp) > maxAgeDifference && curTS != 0)
                {
                    Console.WriteLine("WARNING: ERRONEOUS MEASUREMENT TIMESTAMP!!! ");
                    Console.WriteLine("Last Measurement TS: " + curTS.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("Recieved         TS: " + item.TimeStamp.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("IGNORING OLD MEASUREMENT");
                    events.Clear();
                    curTS = 0;
                    return;
                }

                //If a new measurement comes in and is too new, reset the queue and add it
                if ((item.TimeStamp - curTS) > maxAgeDifference && curTS != 0)
                {
                    Console.WriteLine("WARNING: ERRONEOUS MEASUREMENT TIMESTAMP!!! ");
                    Console.WriteLine("Last Measurement TS: " + curTS.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("Recieved         TS: " + item.TimeStamp.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("CLEARING QUEUE");
                    events.Clear();
                    events.Add(item);
                    curTS = item.TimeStamp;
                    return;
                }

                if (events.Count == 0)
                {
                    events.Add(item);
                    curTS = item.TimeStamp;
                }
                else
                {
                    int index = 0;
                    //Start at end of queue
                    for (int i = events.Count - 1; i >= 0; i--)
                    {
                        if (item.TimeStamp > events[i].TimeStamp)
                        {
                            index = i + 1;
                            break;
                        }
                    }
                    if (index == events.Count)
                        curTS = item.TimeStamp;
                    events.Insert(index, item);
                }
            }
        }
        public void AddEvent(ITimestampedEventQueueItem item)
        {
            //find the index by doing a simple linear search

            lock (this)
            {
                //the earliest items are at the top of this queue and the latest items on the bottom.
                //the goal is to start from the bottom and insert right after the first item that is
                //before us.
                if (events.Count >= capacity)
                {
                    //if (!emptyWithoutError)
                    //Console.WriteLine("EVENT QUEUE IS FULL. OLDEST MEASUREMENT PURGED.");
                    Pop();
                }

                //Chuck measurements that are too old
                if ((curTS - item.TimeStamp) > maxAgeDifference && curTS != 0)
                {
                    Console.WriteLine("WARNING: ERRONEOUS MEASUREMENT TIMESTAMP!!! ");
                    Console.WriteLine("Last Measurement TS: " + curTS.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("Recieved         TS: " + item.TimeStamp.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("IGNORING OLD MEASUREMENT");
                    events.Clear();
                    curTS = 0;
                    return;
                }

                //If a new measurement comes in and is too new, reset the queue and add it
                if ((item.TimeStamp - curTS) > maxAgeDifference && curTS != 0)
                {
                    Console.WriteLine("WARNING: ERRONEOUS MEASUREMENT TIMESTAMP!!! ");
                    Console.WriteLine("Last Measurement TS: " + curTS.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("Recieved         TS: " + item.TimeStamp.ToString("F6") + " Type: " + item.DataType);
                    Console.WriteLine("CLEARING QUEUE");
                    events.Clear();
                    events.Add(item);
                    curTS = item.TimeStamp;
                    return;
                }

                if (events.Count == 0)
                {
                    events.Add(item);
                    curTS = item.TimeStamp;
                }
                else
                {
                    int index = 0;
                    //Start at end of queue
                    for (int i = events.Count - 1; i >= 0; i--)
                    {
                        if (item.TimeStamp > events[i].TimeStamp)
                        {
                            index = i + 1;
                            break;
                        }
                    }
                    if (index == events.Count)
                    {
                        curTS = item.TimeStamp;
                    }
                    events.Insert(index, item);
                }
            }
        }