I have a backgroundworker in a C++ .NET forms application which runs async. In the DoWork function of this backgroundworker I want to add rows to a datagridview, however I can’t really figure out how to do this with BeginInvoke as the code I have doesn’t seem to work.
The code I have
delegate void invokeDelegate(array<String^>^row);
....
In the DoWork of the backgroundworker
....
array<String^>^row = gcnew array<String^>{"Test", "Test", "Test"};
if(ovlgrid->InvokeRequired)
ovlgrid->BeginInvoke(gcnew invokeDelegate( this, &Form1::AddRow), row);
....
void AddRow(array<String^>^row)
{
ovlgrid->Rows->Add( row );
}
The error I get is:
An unhandled exception of type
‘System.Reflection.TargetParameterCountException’ occurred in
mscorlib.dllAdditional information: Parameter count mismatch.
When I change to code to not pass any parameters it just works, the code than becomes:
delegate void invokeDelegate();
...
In the DoWork function
...
if(ovlgrid->InvokeRequired)
ovlgrid->BeginInvoke(gcnew invokeDelegate( this, &Form1::AddRow));
...
void AddRow()
{
array<String^>^row = gcnew array<String^>{"test","test2","test3"};
ovlgrid->Rows->Add( row );
}
The problem though is that I want to pass parameters.
I was wondering what I’m doing wrong which causes the parametercountexception and how to fix this?
The problem you’ve run into is
BeginInvoketakes an array of parameters and you pass it an array which happens to be the one parameter.Therefore,
BeginInvoketakes this to mean you have 3 string parameters to the method:"test","test2", and"test3". You need to pass an array containing just yourrow: