Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Invoke() delegate with string array as an argument (winforms)

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?

like image 664
A-D-D-F_Toxic Avatar asked May 23 '13 23:05

A-D-D-F_Toxic


People also ask

What is C in simple words?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C or C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

Assuming sUsernames is a string[] then yes, you need to call it with

Invoke(_dLoadUserSelect, new object[] { sUsernames });

.Net arrays are covariant, so this assignment is valid:

string[] sUsernames = new[] { "a", "b", "c" };
object[] objs = sUsernames;

and when calling a method with params arguments, the array is passed directly instead of being passed as the first element in an argument array. You need to manually create the argument array for Invoke to get the behaviour you expect.

like image 138
Lee Avatar answered Sep 19 '22 12:09

Lee