c – 使用BeginInvoke时的参数计数不匹配异常
我在C .NET表单应用程序中有一个后台工作程序,它运行异步.在这个后台工作者的DoWork函数中,我想向datagridview添加行,但是我无法弄清楚如何使用BeginInvoke执行此操作,因为我的代码似乎不起作用.
我的代码 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 ); } 我得到的错误是:
当我更改为代码以不传递任何参数它只是工作,代码而不是: 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 ); } 但问题是我想传递参数. 解决方法
你遇到的问题是
BeginInvoke takes an array of parameters,你传递一个恰好是一个参数的数组.
因此,BeginInvoke认为这意味着您有3个字符串参数:“test”,“test2”和“test3”.您需要传递一个只包含您的行的数组: array<Object^>^ parms = gcnew array<Object^> { row }; ovlgrid.BeginInvoke(gcnew invokeDelegate(this,parms); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |