Search code examples
c#user-interfacedispatcher

Gui beingInvoke issue with params


Below is a segment of code from a client handler class.

I know I am passing the wrong information as noted in the comment below. I just need to know exactly what I should pass to make it work.

public static void add2ClientList(string s)
    {
        MainWindow.mainWindow.ClientListBox.Items.Add(s);

    }

    Action<string> addToClientListBox = new Action<string> (add2ClientList);

    public void addClientToPool(Client c)
    {
        if (ClientPool == null)
        {
            ClientPool = new Client[] { c };
            uiDispatcher.BeginInvoke(addToClientListBox, DispatcherPriority.Background, CancellationToken.None, TimeSpan.Zero, c.getClientIp());
// above is the issue apparently I am passing the wrong params
            return;
        }
        List<Client> temp = new List<Client>();
        foreach (Client cc in ClientPool)
        {
            temp.Add(cc);
        }
        temp.Add(c);
        ClientPool = temp.ToArray();
        uiDispatcher.BeginInvoke(addToClientListBox, DispatcherPriority.Background, CancellationToken.None, TimeSpan.Zero, c.getClientIp());

    }

Solution

  • The only thing that appears to be missing is your param to pass to the delegate, I assume you want the following:

    uiDispatcher.CurrentDispatcher.BeginInvoke(addToClientListBox, new object[]{"ParamString"}, DispatcherPriority.Background);
    

    If that's not exactly what you were asking let me know :)