locked
BindingList cross thread invoked call confusion RRS feed

  • Pergunta

  • 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?

    quinta-feira, 4 de outubro de 2012 23:43

Respostas

  • "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.

    • Sugerido como Resposta Bob Shen terça-feira, 9 de outubro de 2012 08:31
    • Marcado como Resposta Bob Shen terça-feira, 16 de outubro de 2012 09:05
    sexta-feira, 5 de outubro de 2012 01:48

Todas as Respostas

  • 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 :/
    sexta-feira, 5 de outubro de 2012 00:48
  • Try with Items.Clear() method:

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


    Mitja

    sexta-feira, 5 de outubro de 2012 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.

    sexta-feira, 5 de outubro de 2012 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.

    • Sugerido como Resposta Bob Shen terça-feira, 9 de outubro de 2012 08:31
    • Marcado como Resposta Bob Shen terça-feira, 16 de outubro de 2012 09:05
    sexta-feira, 5 de outubro de 2012 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.

    sexta-feira, 5 de outubro de 2012 02:01
  • You have to bind and unbind the listbox on the UI thread. 
    sexta-feira, 5 de outubro de 2012 02:38
  • How would I do that from a thread???
    sexta-feira, 5 de outubro de 2012 02:41
  • You wouldn't.
    sexta-feira, 5 de outubro de 2012 02:42
  • So I can't use a bindinglist from a thread? that makes no sense at all.
    sexta-feira, 5 de outubro de 2012 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.
    sexta-feira, 5 de outubro de 2012 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...

    quarta-feira, 17 de outubro de 2018 11:28
  • Invoking Binding List does the trick:

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

    terça-feira, 4 de fevereiro de 2020 20:31