【发布时间】:2013-01-22 23:42:31
【问题描述】:
我经常发现并没有明确说明是什么确切的集合导致了这种类型的异常。这是真的还是应该很明显?也许我只是不明白如何正确解释异常消息..
我特别想知道这个。它指的是什么收藏?
事件委托的参数很简单(对象发送者),引发的事件传递空参数。虽然引发事件的类本身继承了一个列表:
public class TimeSerie : List<BarData>
这里是否清楚“集合”是指引发事件的对象,还是可以是另一个对象?可以说是动态更改的方法的事件处理程序集合吗?还是会产生不同的异常?
************** 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)
UserControl 发生异常:
public class SomeUserControl
private void InvokeOnUpdate(object sender)
{
this.Invoke(new GenericInvoker(Method)); // << Exception here!
}
private void Method() {...}
编辑: 添加了一些代码。有点简化,但认为它包含相关位。
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;
}
【问题讨论】:
-
告诉我们
Method的定义。异常的根本原因不在该行上。 -
好的,添加了我认为相关的内容。
-
这里仍然没有引用任何集合的代码。
instrument.DataSeries.GetTimeSerieByInterval没有显示,ClearAndLoadAllHistory也没有显示,这两种方法听起来都像是与集合交互的方法。 -
我不相信你知道什么是相关的,什么是不相关的(不要说粗鲁,只是说;你不知道问题出在哪里)。查看顶级异常的 InnerException 属性,看看它是否会引导您。
-
@Ed S;我可以,但这不是我得到例外的地方。也许它可以被复制,但它不是一个常见的例外。但是请再次阅读我的问题,我并不是真的在询问错误,而是如何以及是否可以解释异常消息以识别集合。这是我最希望拥有的理解。
标签: c# .net multithreading invoke invalidoperationexception