none
Threading Problem with BindingSource? RRS feed

  • Question

  • Hi, in my application (.net 2.0), I have a grid (infragistics) which is binding to a typed BindingList<T> through a BindingSource.
    However, the BindingList<T> that it is binding to will be updated periodically by another thread in the background.

    Occasionally, I am getting this exception when there is an update to the BindingList by the background thread...

      <Description>An exception of type 'System.InvalidOperationException' occurred and was caught.</Description>
      <DateTime>2008-06-25 19:55:19Z</DateTime>
      <ExceptionType>System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
      <Message>BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refer back to BindingSource.</Message>
      <Source>System.Windows.Forms</Source>
      <HelpLink />
      <Property name="Data">System.Collections.ListDictionaryInternal</Property>
      <Property name="TargetSite">Int32 get_Count()</Property>
      <StackTrace>   at System.Windows.Forms.BindingSource.get_Count()
       at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
       at System.ComponentModel.ListChangedEventHandler.Invoke(Object sender, ListChangedEventArgs e)
       at System.Windows.Forms.BindingSource.OnListChanged(ListChangedEventArgs e)
       at System.Windows.Forms.BindingSource.InnerList_ListChanged(Object sender, ListChangedEventArgs e)
       at System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e)
       at System.ComponentModel.BindingList`1.SetItem(Int32 index, T item)
    ...


    Can someone please tell me what I did wrong? Since the background thread is not accessing the UI directly... I assume that I can access the reference of the BindingList directly (i.e. without Invoke/InvokeRequired)?

    Thanks.

    kai
    Friday, June 27, 2008 2:02 AM

Answers

  • I can't see your code, so I'm only guessing.  But a BindingList<T> suggests to me that it might be enumerated by your code or by the BindingSource.  In most lists, there are documented warnings about enumerations being inherently non-thread-safe.  Documentation goes on to say consider locking the entire list when enumerating.

    In short, your thread should not update BindingList<T> if its being enumerated.  Is there any chance that this is happening? 
    • Marked as answer by jack 321 Wednesday, July 2, 2008 2:19 AM
    Friday, June 27, 2008 2:24 AM
  • The background thread cannot access the UI either directly OR indirectly. In your case it is indirect. Updating the list raises an event that updates the UI. In other words, you are not allowed to access the binding list from the worker thread.
    Kevin Kerr - http://blog.quantumbitdesigns.com
    • Marked as answer by jack 321 Wednesday, July 2, 2008 2:20 AM
    Friday, June 27, 2008 5:28 PM

All replies

  • I can't see your code, so I'm only guessing.  But a BindingList<T> suggests to me that it might be enumerated by your code or by the BindingSource.  In most lists, there are documented warnings about enumerations being inherently non-thread-safe.  Documentation goes on to say consider locking the entire list when enumerating.

    In short, your thread should not update BindingList<T> if its being enumerated.  Is there any chance that this is happening? 
    • Marked as answer by jack 321 Wednesday, July 2, 2008 2:19 AM
    Friday, June 27, 2008 2:24 AM
  • The background thread cannot access the UI either directly OR indirectly. In your case it is indirect. Updating the list raises an event that updates the UI. In other words, you are not allowed to access the binding list from the worker thread.
    Kevin Kerr - http://blog.quantumbitdesigns.com
    • Marked as answer by jack 321 Wednesday, July 2, 2008 2:20 AM
    Friday, June 27, 2008 5:28 PM