CodeSmith Community
Your Code. Your Way. Faster!

Thread safe IBindingList?

Latest post 11-14-2007 12:21 AM by marioh. 1 replies.
  • 11-13-2007 11:47 PM

    • marioh
    • Top 50 Contributor
    • Joined on 02-21-2006
    • Boston, MA
    • Posts 90
    • Points 2,580

    Thread safe IBindingList?

    Hi All,

    I am trying to update a collection bound to a DataGridView through a BindingSource and using a BindingNavigator for my grid commands.

    One of these commands updates the data bound items in bulk in a separate thread. This causes a cross-thread operation exception to be thrown by the binding navigator control when the application is compiled in debug mode.

    The issue is that some of these controls subscribe to the item and list changed events of the IBindingList (TList in my case) which are fired from a separate thread.

    Has anyone worked on updating the ListBase class to support a synchronized invoke in the context of win UIs?

    i.e. something like http://windowsclient.net/articles//asyncbindinglist.aspx

    Thanks

    • Post Points: 5
  • 11-14-2007 12:21 AM In reply to

    • marioh
    • Top 50 Contributor
    • Joined on 02-21-2006
    • Boston, MA
    • Posts 90
    • Points 2,580

    Re: Thread safe IBindingList?

    a few notes:

    1. I am not using the latest version of the templates (explains why I wouldn't have seen it if someone already added the support for async updates)
    2. I made this work by adding constructors to all the collections take an ISynchronizeInvoke param as well as exposed it through a property on the ListBase collection.
    3. I then implemented an override for the BindingList<T>.OnListChanged() method in ListBase (below).
    4. When initializing a TList<> or VList<> (or after getting it back from the DAL), I set the ThreadSyncEventInvoke to my main windows form instance.
    protected override void OnListChanged(ListChangedEventArgs e)

    {

    if (ThreadSyncEventInvoke != null

    && ThreadSyncEventInvoke.InvokeRequired)

    {

    ThreadSyncEventInvoke.BeginInvoke(

    new ListChangedDelegate(base.OnListChanged), new object[ { e });

    }

    else

    {

    base.OnListChanged(e);

    }

    }

    Any thoughts?

    • Post Points: 5
Page 1 of 1 (2 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems