我无法找出导致此错误的原因:
在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。
这是我的(已剥离)代码:
private: delegate void MyDelegate(Object^ openFileDialog1);
ListView^ myDelegate;
private: void import_links(Object^ openFileDialog1) {
myDelegate = (gcnew System::Windows::Forms::ListView());
myDelegate->BeginInvoke( gcnew MyDelegate( this, &Form1::import_links ), openFileDialog1);
//do some work here
}
private: System::Void Import_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e) {
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
Thread^ importThread = gcnew Thread(gcnew ParameterizedThreadStart(this,&Form1::import_links));
importThread->Start(openFileDialog1);
}
}
请让我知道解决方案。
答案 0 :(得分:2)
myDelegate = (gcnew System::Windows::Forms::ListView());
本声明的基本问题:
您已经拥有对正确线程所拥有的对象的引用。它是this
。所以正确的代码看起来像:
void import_links(Object^ openFileDialog1) {
if (this->InvokeRequired) {
this->BeginInvoke( gcnew MyDelegate( this, &Form1::import_links ), openFileDialog1);
}
else {
//do some work here
}
}
但请注意最终的谬误,你创建了一个工作线程,而仅它所做的就是调用this->BeginInvoke()
。这需要几分之一微秒。创建一个线程来做这么少的工作永远不会有用。
重构您的代码,使用BackgroundWorker。让其DoWork事件处理程序仅执行昂贵的操作,例如导入文件。让其RunWorkerCompleted事件仅执行需要在UI线程上发生的事情,例如显示导入的结果并隐藏“我正在处理它”通知。