System.Linq error InvalidOperationException: on Queue.Average

Using a Queue to average a collection of 9 float values. Once in a while (it usually works!) I am getting the following error

The offending line is the last in this code excerpt
I believe the error is happening because my queue is being modified at the same time as the iteration.
And I need ‘locks’ around the reads and updates. Can anyone help me implement this?

private bool OnPersonUpdated(IEvent evt)
{
    Event_Update castEvent = evt as Event_Update;
    if (castEvent != null)
    {
        if (peopleDict.ContainsKey(castEvent.id))
        {
            float xVel = castEvent.velX;
            GameObject cubeToMove = peopleDict[castEvent.id];
            if (xVel > 0)
            {
                float xPos = -1f * positionForPerson(castEvent.person);

                float dif = xPos - _prevX;
                if (dif < .5f)
                {
                    _posQueue.Enqueue(xPos);
                }
                if (_posQueue.Count >= 10)
                {
                    _posQueue.Dequeue();
                }
                float avPos = _posQueue.Average();

You should invest the time reading some blogs and docs about locking and threading. It’s important info.

In your case, you want to create a lock objectprivate void object _padlock = new object(); and surround each read or write access of your queue with a lock statement

lock (_padlock)
{
    _posQueue.Enqueue( stuff );
}

Be sure to use the same locking object on each point of access.

thanks…advice taken. Noob question

I found a ‘lockable Queue’ implementation for Unity but is missing an Average method.
Any chance someone can help?