locked
BindingList cross thread invoked call confusion RRS feed

  • 问题

  • I'm using this method to check and invoke a BindingList which is attached to a listbox.

            public void ClientListClear()
            {
                if (ToolMessenger.Instance.listBoxClients.InvokeRequired)
                {
                    ToolMessenger.Instance.Invoke((Action)(() => clientList.Clear()));
                }
                else
                {
                    clientList.Clear();
                }
            }

    From a thread I'm calling

    ClientListClear()

    However it's still throwing an exception tellin g me I'm trying to make a cross thread call to the listbox when clearly I'm not. This has baffled me beyond beliefe, I use this type of invoking all the time without issues, any ideas whats going on?

    2012年10月4日 23:43

答案

  • "However it's still throwing an exception tellin g me I'm trying to make a cross thread call to the listbox when clearly I'm not."

    Your listbox is bound to your bindinglist.  Any change to the bindinglist tries to change the listbox on the same thread.  Unbind the listbox, update the bindinglist and when finished, rebind the listbox.

    • 已建议为答案 Bob Shen 2012年10月9日 8:31
    • 已标记为答案 Bob Shen 2012年10月16日 9:05
    2012年10月5日 1:48

全部回复

  • Another weird thing is when I run this without the debugger, it works fine (at least on this part I  posted above), I do not get this exception, however there are other parts of the application I need to debug but I am unable to because of the above issue, this is mind boggling :/
    2012年10月5日 0:48
  • Try with Items.Clear() method:

    private void ClientListClear()
    {
       ToolMassenger.Instance.Invoke ((Action) (() =>clientList.Items.Clear()));
    }


    Mitja

    2012年10月5日 1:08
  • Try with Items.Clear() method:

    private void ClientListClear()
    {
       ToolMassenger.Instance.Invoke ((Action) (() =>clientList.Items.Clear()));
    }


    Mitja

    Hi Mitja, a BindingList does not have an "Items" property.

    2012年10月5日 1:15
  • "However it's still throwing an exception tellin g me I'm trying to make a cross thread call to the listbox when clearly I'm not."

    Your listbox is bound to your bindinglist.  Any change to the bindinglist tries to change the listbox on the same thread.  Unbind the listbox, update the bindinglist and when finished, rebind the listbox.

    • 已建议为答案 Bob Shen 2012年10月9日 8:31
    • 已标记为答案 Bob Shen 2012年10月16日 9:05
    2012年10月5日 1:48
  • "However it's still throwing an exception tellin g me I'm trying to make a cross thread call to the listbox when clearly I'm not."

    Your listbox is bound to your bindinglist.  Any change to the bindinglist tries to change the listbox on the same thread.  Unbind the listbox, update the bindinglist and when finished, rebind the listbox.

    Ok I tried what you said:

    public void UnbindClientList()
            {
                if (listBoxClients.InvokeRequired)
                {
                    listBoxClients.Invoke((Action)(() => UnbindClientList()));
                }
                else
                {
                    listBoxClients.DataSource = null;
                }
            }

    I then call clientList.Clear() directly, and bam I get the exception... still.

    2012年10月5日 2:01
  • You have to bind and unbind the listbox on the UI thread. 
    2012年10月5日 2:38
  • How would I do that from a thread???
    2012年10月5日 2:41
  • You wouldn't.
    2012年10月5日 2:42
  • So I can't use a bindinglist from a thread? that makes no sense at all.
    2012年10月5日 2:45
  • sSo I can't use a bindinglist from a thread? that makes no sense at all.
    I'm sorry.  We aren't communicating.  Perhaps someone else can explain it better.
    2012年10月5日 2:47
  • Hi man. Did you ever solve this.

    Im' getting the same, with the same level of confusion.

    Been using this approach for years, and now it seemingly randomly blows up...

    2018年10月17日 11:28
  • Invoking Binding List does the trick:

    https://stackoverflow.com/questions/10156991/inotifypropertychanged-causes-cross-thread-error

    2020年2月4日 20:31