我有一个带有ListView的应用程序,其中包含一些我想传递给BackgroundWorker的文件名和文件路径。我基本上希望工作线程知道这个ListView包含这种或那种方式的字符串。
在修补此问题并进行搜索时,我尝试制作public ref Class
,以便将其发送到RunWorkerAsync()
函数,这样我就无法将转换为System :: Object 错误:
public ref class FileInfoContainer {
public:
cli::array<System::String^>^ filepaths;
cli::array<System::String^>^ filenames;
System::String^ outfile;
FileInfoContainer(cli::array<System::String^>^ filepaths, cli::array<System::String^>^ filenames, System::String^ outfile);
};
我还尝试将整个Form作为参数传递给RunWorkerAsync()
,在调试期间,它确实将表单所包含的每个数据传递到System::Object
。问题是如果我尝试通过这个对象访问我的表单元素,我得到一个不是System :: Object 的成员错误:
private: System::Void compworker_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
System::Object^ arg = e->Argument;
arg->button1 //this fails
MainForm form1 = (MainForm)e->Argument; //this also fails, even though I've seen it suggested in numerous threads
}
关于如何将我的参数传递给工作线程的任何好建议?