I have the following code which is called every 3 seconds continuously from a thread
public class SomeClass
{
List<Person> _list;
public SetList(List<Preson> list)
{
_list = list;
}
private void WorkToBeDoneEverythreeSeconds()
{
var filteredList= _list.Where(x= x.IsConditionValid());
//................Use the filtered list here.........
}
}
_list
is a reference to a C# List
owned by another class passed into this class. The list is updated from a different thread in its owner class. Sometimes updates happening while the Where clause is executed and Invalid operation is thrown.
What is the most efficient way to get a snapshot on the actual list when using the enumerator? I can think of creating another collection from the current collection, but doing this every 3 seconds might not be the best idea?