I often find it's not really specified what exact collection is causing this type of exception. Is that true or should it be obvious? Perhaps I just don't understand how to interpret the exception message properly..
Specifically I wonder about this one. What is the collection it refers to?
The parameter of the event delegate is simply (object sender), and the events raised passes null parameter. Though the class itself that raises the event inherits a list:
public class TimeSerie : List<BarData>
Is it clear here if the "collection" refers to the object that raises the event, or can it be another object? Can it be, say a collection of event handlers to a method that is being dynamically changed? Or would that create a different exception?
************** Exception Text **************
System.InvalidOperationException:
Collection was modified; enumeration operation may not execute.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at SomeNameSpace.SomeUserControl.InvokeOnUpdateHistory(Object sender) in D:\SomePath\SomeUserControl.cs:line 5179
at OtherNameSpace.OtherClass.TimeSerie.HistoryUpdateEventHandler.Invoke(Object sender)
Exception occurs in UserControl:
public class SomeUserControl
private void InvokeOnUpdate(object sender)
{
this.Invoke(new GenericInvoker(Method)); // << Exception here!
}
private void Method() {...}
EDIT: Added some code. A bit simplified, but think it includes relevant bits.
private void Method()
{
if (this.instrument == null) return;
UnRegisterTimeSerieHandlers(this.ts);
this.ts = instrument.DataSeries.GetTimeSerieByInterval(interval);
if (ts != null)
{
RegisterTimeseriesHandlers(ts);
ClearAndLoadAllHistory();
}
}
private void UnRegisterTimeSerieHandlers(TimeSerie ts)
{
if (ts != null)
{
ts.TickUpdate -= InvokeUpdateCurrentBar;
ts.NewBarUpdate -= InvokeUpdateNewBar;
ts.HistoryUpdate -= InvokeOnUpdateHistory;
this.ts = null;
}
}
private void RegisterTimeseriesHandlers(TimeSerie ts)
{
ts.TickUpdate += InvokeUpdateCurrentBar;
ts.NewBarUpdate += InvokeUpdateNewBar;
ts.HistoryUpdate += InvokeOnUpdateHistory;
}