收集奇怪的例外

sha*_*dad 3 wpf exception observablecollection

当我尝试向集合(或任何更改集合的操作)添加/插入/删除时,我得到以下异常.初始化集合,插入的项目不为null,与集合T的类型相同.

谁能给我一个线索,说明为什么会这样?

运行时遇到了致命错误.错误的地址是在0x60f41744线程上0x231c.错误代码是0x80131623.
此错误可能是CLR中的错误,也可能是用户代码的不安全或不可验证部分中的错误.此错误的常见来源包括COM-interop或PInvoke的用户编组错误,这可能会破坏堆栈.

更新:该集合是一个ObservableCollection,我设法得知它发生在集合的通知部分发生了变化.

这发生在具有该TaskScheduler.FromCurrentSynchronizationContext()选项的任务内的UI线程上.

奇怪的是如果我删除this(TaskScheduler.FromCurrentSynchronizationContext())选项add/insert/remove动作,一切似乎都很好.

Han*_*ant 5

错误代码是0x80131623

这是一个非常具体的错误代码,COR_E_FAILFAST.只有一种方法可以生成它,有人称之为Environment.FailFast().

显然,挑战在于找出所谓的代码.首先看一下Windows应用程序事件日志,应该有一条关于它的消息,它给出了调用的主要原因,无论字符串是否传递给FailFast().


应用程序通过System.Environment.FailFast(字符串消息)请求进程终止.
处于
System.Windows.WeakEventE.EffiverEvent(System.Object,System.EventArgs )中System.Windows.WeakEventManager.DeliverEventToList(System.Object,System.EventArgs,ListenerList)的System.Environment.FailFast(System.String
)

是的,该代码中有一个Assert().我只是发布我在参考源中看到的内容,我对你的代码知之甚少,看看你做错了什么.除了线程之外肯定是触发此类问题的好方法,ObservableCollection完全是线程不安全的,必须受到锁的保护.

   // if the event isn't handled, something is seriously wrong.  This
   // means a listener registered to receive the event, but refused to
   // handle it when it was delivered.  Such a listener is coded incorrectly.
   if (!handled)
   {
       Invariant.Assert(handled,
                   SR.Get(SRID.ListenerDidNotHandleEvent),
                   SR.Get(SRID.ListenerDidNotHandleEventDetail, iwel.GetType(), managerType));
   }
Run Code Online (Sandbox Code Playgroud)

  • 那时必须有人向 Microsoft 打开一个错误。这完全是错误的行为 - 对于这种情况,存在已知的定义异常。此处理程序应替换为正确的异常(从 InvalidOperationException 到其他异常)。 (2认同)