Queue.Enqueue and IndexOutOfRangeException

Hong 1,211 Reputation points
2024-06-12T09:24:45.5533333+00:00

I have been chasing a strange occasional IndexOutOfRangeException for a while and pinned it on the following line:

myQueue.Enqueue(myObject)

where:

protected Queue<MyObject> myQueue{ get; set; }

I don't know how this is possible. The stack trace:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
 Stack Trace:
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowIndexOutOfRangeException() + 0x25
   at ClassLibraryFoo.NVT.DecoderWin.<MethodFoo>d__38.MoveNext() + 0x46f

Could anyone shed some light on this?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Viorel 118.2K Reputation points
    2024-06-12T11:53:37.8433333+00:00

    I think that the exception is caused by threading issues.

    Maybe you can use a mutex, or a ConcurentQueue, or a queue returned by Queue.Synchronized(myQueue).

    Also see the remark about the enumerators:

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.