C# 有时我会在BindingsCollection中获得System.ArgumentOutOfRangeException

C# 有时我会在BindingsCollection中获得System.ArgumentOutOfRangeException,c#,.net,winforms,exception,C#,.net,Winforms,Exception,有时,当我想显示一个用户控件并设置其绑定时,我会得到一个System.ArgumentOutOfRangeException。当我单击“继续”时,操作将中止,但当我再次执行相同操作时,用户控件将正确显示。我无法重现这个问题 我读过一些问题,其中BingdinsCollection在更新过程中发生了更改。但是我不能指出我的项目中的一段特定代码。这和窗户把手有关吗 ************** Exception Text ************** System.ArgumentOutOfRan

有时,当我想显示一个用户控件并设置其绑定时,我会得到一个System.ArgumentOutOfRangeException。当我单击“继续”时,操作将中止,但当我再次执行相同操作时,用户控件将正确显示。我无法重现这个问题

我读过一些问题,其中BingdinsCollection在更新过程中发生了更改。但是我不能指出我的项目中的一段特定代码。这和窗户把手有关吗

************** Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at System.Windows.Forms.BindingsCollection.get_Item(Int32 index)
   at System.Windows.Forms.Control.UpdateBindings()
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.ContainerControl.OnCreateControl()
   at System.Windows.Forms.UserControl.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   .. my click

是否在另一个线程上设置BindingSource?或者在另一个线程上执行任何可能导致数据绑定集合在枚举时发生更改的操作

从框架来源来看,这似乎是实现这一点的唯一途径

从Control.cs

 private void UpdateBindings()
    {
      for (int index = 0; index < this.DataBindings.Count; ++index)
        BindingContext.UpdateBinding(this.BindingContext, ((BindingsCollection) this.DataBindings)[index]);
    }

从异常堆栈跟踪中,看起来您正在更改控件的可见性,但是绑定到该控件的ArrayList没有该控件的正确条目


我会尝试改变你行动的顺序。例如,如果您正在绑定并显示,我会反转该过程。

如果您询问诸如调试此过程或类似过程是什么之类的问题,而不是信息很少的是/否问题,您的问题会得到改进。这里没有足够的问题来帮助您。你需要发布失败的代码。七年后,我不知从何处收到同样的随机无意义异常,只是有时,除了一些内部设计器的堆栈条目外,没有更多的堆栈条目,并在应用程序处停止。运行。。。我感觉到你了,兄弟。首先我创建控件,设置绑定并将其设置为其父控件,最后我显示它。我们只想显示实际数据,而不是默认值/空值。我们使用颜色来显示状态,不希望一直显示状态更改。好的,听起来在某些情况下绑定是不正确的。我看不到您正在使用的代码,但我会确保在数组列表和用于绑定控件的索引周围内置了大量范围检查。添加绑定和显示屏幕发生在同一线程中。数据本身来自另一个线程。在发生异常的usercontrol中,我不删除绑定,只添加它们。我也不知道异常发生在哪个控件上,无法复制它。
public Binding this[int index]
{
  get
  {
    return (Binding) this.List[index];
  }
}