I am attempting to use this.Invoke() from a separate thread to access controls on my form. I am Invoking a delegate pointing to a method with a string[] as an argument.
A few lines regarding my delegate declaration:
public delegate void delVoidStringArray(string[] s);
public delVoidStringArray _dLoadUserSelect = null;
_dLoadUserSelect = LoadUsers;
Invoking the delegate from a separate thread:
Invoke(_dLoadUserSelect, sUsernames);
And the method called to work with the controls on the form
private void LoadUsers(string[] users)
{
//Load the list of users into a ListBox
lstUsers.Items.AddRange(users);
//Load the state of a CheckBox on the form
chkUserAlways.Checked = Properties.Settings.Default.PreferDefaultUser;
}
This normally works with the rest of my delegates with various arguments (string, Control, Form, and no arguments), but whenever I call this Invoke() line, I get an error: "Parameter count mismatch."
I think what's happening is that my string array is being boxed into an object array and the delegate is trying to pass these strings as separate arguments to the method. So if the string array had "Bob" "Sally" and "Joe", it is attempting to call LoadUsers as
LoadUsers("Bob", "Sally", "Joe");
which obviously doesn't match the signature.
Does this sound like something that might happen? How could I work around this issue?