Search code examples
c#wpfnugetinvoke

How to add "Invoke" method to my project?


I can't find the right NuGet packet to install the invoke method. I have a wpf GUI and I have a separate thread that needs to update items in a listBox. I need the "Invoke" method in order to change the items in the listBox.

    public void displayPlayers(string players)
    {
        //spliting all the names.
        string[] names = players.Split(", ".ToCharArray());

        //Displaying the names.
        foreach (string name in names)
            this.Invoke((MethodInvoker)(() => playersListBox.Items.Add(name)));
    }

Solution

  • Use the Dispatcher.Invoke() method. It is accessible via Application class (see more) or on the control itself. For more info see: Dispatcher.Invoke