Reputation: 176
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.dll
Additional 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?
Upvotes: 4
Views: 1915
Reputation: 64098
The problem you've run into is BeginInvoke
takes an array of parameters and you pass it an array which happens to be the one parameter.
Parameters
method
Type:
System.Delegate
The delegate to a method that takes parameters specified in
args
, which is pushed onto the Dispatcher event queue.args
Type:
System.Object[]
An array of objects to pass as arguments to the given method. Can be
null
.
Therefore, BeginInvoke
takes this to mean you have 3 string parameters to the method: "test"
, "test2"
, and "test3"
. You need to pass an array containing just your row
:
array<Object^>^ parms = gcnew array<Object^> { row };
ovlgrid.BeginInvoke(gcnew invokeDelegate(this, &Form1::AddRow), parms);
Upvotes: 2