I use a thread to execute some process on a machine. Eventually, the progress is reported back in an other thread. To update the GUI with the status of the process, I use a delegate like this:
public delegate void UpdateProgressDelegate(string description, int scriptnumber);
public void UpdateProgress(string description, int scriptnumber) {
if (treeView.InvokeRequired) {
treeView.Invoke(new UpdateProgressDelegate(UpdateProgress), description, scriptnumber);
return;
}
// Update the treeview
}
And to call this delegate I use:
form.UpdateProgress("Ready", 3);
When the Invoke is called, I get a TargetParameterCountException: Parameter count mismatch.
I thought I could fix this by placing the string and int parameters in a single object like this:
public delegate void UpdateProgressDelegate(object[] updateinfo);
public void UpdateProgress(object[] updateinfo) {
string description = (string) updateinfo[0];
int scriptnumber = (int) updateinfo[1];
if (treeView.InvokeRequired) {
treeView.Invoke(new UpdateProgressDelegate(UpdateProgress), new object[] { description, scriptnumber });
return;
}
// Update the treeview
}
And to call it I use:
form.UpdateProgress(new object[] {"Ready", 3});
But this doesn’t work either. I keep getting the same TargetParameterCountException. Any ideas how I could fix this? Thanks in advance!
I would say: do it the easy way:
or (equally):
This gives you static-checking at the compiler, and IIRC
MethodInvokeris checked explicitly, and called withInvoke()rather thanDynamicInvoke(), making it faster too.Re why it doesn’t work; in your example with:
you are actually passing two parameters; to disambiguate and pass a single array to a
paramshere, you need to double-wrap it:Basically, the outer array is the “array of all the parameters”, which contains a single element, which is the array that we wan’t to pass as the first parameter (
updateinfo).