TargetParameterCountException: Parameter count mismatch on invoke delegate

I have the following code (removed unrelated)

//top of class declaration
private delegate void UpdateFormElements(string status, bool addEmptyRow);

//inside a function in my class
if(lboImages.InvokeRequired)
{
   lboImages.Invoke((UpdateFormElements)delegate { UpdateListBox("some text", true); });
}

private void UpdateListBox(string line, bool addEmptyRow)
{
    lboImages.Items.Add(line);
    if (addEmptyRow)
    {
        lboImages.Items.Add("");
    }
}

Basically I'm trying to pass two parameters to the UpdateListBox function to test whether to add an empty line or not to my listbox, but I am getting the error in the title. I have tried putting the two values in an object[] but it doesn't seem to change anything as I still get the error.

I'm still new to using threads so not really sure where I'm going wrong here.

Jon Skeet
people
quotationmark

It's not clear why you're trying to use an anonymous method here. The problem is that you're creating a delegate type with two parameters, but you're not passing arguments (values for those parameters) into Invoke.

I suspect you just want:

lboImages.Invoke((UpdateFormElements) UpdateListBox, "some text", true));

That uses a method group conversion to create an UpdateFormElements delegate, and provides it the two arguments it needs.

Alternatively, you could just use a lambda expression:

MethodInvoker invoker = () => UpdateListBox(line, addEmptyRow);
lboImages.Invoke(invoker);

people

See more on this question at Stackoverflow