How to manage a changing List while performing enumeration?

c#, enumeration, list, multithreading

Solution

Something like this can be used with `ConcurrentQueue<T>` in .Net 4.

ConcurrentQueue<Note> Queue = new ConcurrentQueue<Note>();

void onClick()
{
  Queue.Enqueue(theClickedNote);

  // start Play on another thread if necessary
}

void Play()
{
  if (Playing) return;

  Note note;
  while(Queue.TryDequeue(out note))
  {
     note.Play();
  }
}

ConcurrentQueue is thread-safe, so no locking needs to be implemented.

Problem

I have a list of objects (musical notes) that is enumerated on a separate thread as they are played. I am doing this so that I can keep the UI thread responsive. whilst a note is playing (as part of an enumeration) how can I allow for the fact that a new note may of been added to the List (without the obvious collection modified exception). I know I could copy the list to a temporary list and enumerate that, but I actually want the list to grow as a user selects more (and this will happen whilst the first note is playing etc). psuedo logic as is: ``` onClick() { Queue.Add(theClickedNote) Queue.Play() <-- on another thread } Play() { if(Playing==true){return ;} foreach(note theNote in Queue) { Note.Play(); Queue.Remove(theNote); } } ``` As you can see in the above, each Click event adds a note to the Queue and then invokes a play method on the queue. the queue enumerates the notes and plays each one in turn before removing the note I hope I have explained what I am trying to do clearly?

Original source