Public ObservableCollection<T> SharedObj=new ObservableCollection<T>();
Thread mainThread = new Thread(() => MainThreadMethod(SharedObj);
mainThread.Start();
private DispatcherTimer _graphPlottingTimer=new DispatcherTimer();
_graphPlottingTimer.Tick += new EventHandler(_graphPlottingTimer_Tick);
_graphPlottingTimer.Interval = TimeSpan.FromMilliseconds(100);
_graphPlottingTimer.Start();
private void MainThreadMethod(ObservableCollection<T> obj)
{
//here i am adding rows in obj .
}
void _graphPlottingTimer_Elapsed(object sender, ElapsedEventArgs e)
{
private List<T> refinedList=new List<T>();
//I am getting a Collection Modify Exception on the below line
refinedList =SharedObj.Where(condition).ToList();
}
我在上面的_graphPlottingTimer_Elapsed方法的最后一行收到 Collection modify 异常。
我尝试了 lock 和 Dispatcher.CurrentDispatcher.BeginInvoke 与 _graphPlottingTimer_Elapsed 但它仍然给出相同的异常。