private static Thread thread;
private static List<object> items = new List<object>();
static Program()
{
thread = new Thread(() =>
{
foreach (var item in items)
{
item.Name = "ABC";
}
Thread.Sleep(3600);
});
thread.Start();
}
private static void Main(string[] args)
{
var item = items.Where(i => i.Name == "ABC").FirstOrDefault();
if(item != null)
{
items.Remove(item);
}
}
As you see there is thread get started on application started using List perform some operation on the list, and the same List is used by main thread.
when code inside foreach loop of new thread is executing and at the same time foreach loop also get executed it throws error that u can't modify the list.
Any idea how to overcome the issue?
Thanks