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日 下午 11: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日 上午 08:31
    • 已標示為解答 Bob Shen 2012年10月16日 上午 09:05
    2012年10月5日 上午 01: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日 上午 12:48
  • Try with Items.Clear() method:

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


    Mitja

    2012年10月5日 上午 01: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日 上午 01: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日 上午 08:31
    • 已標示為解答 Bob Shen 2012年10月16日 上午 09:05
    2012年10月5日 上午 01: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日 上午 02:01
  • You have to bind and unbind the listbox on the UI thread. 
    2012年10月5日 上午 02:38
  • How would I do that from a thread???
    2012年10月5日 上午 02:41
  • You wouldn't.
    2012年10月5日 上午 02:42
  • So I can't use a bindinglist from a thread? that makes no sense at all.
    2012年10月5日 上午 02: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日 上午 02: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日 下午 08:31