2011-12-01 43 views
0

我不确定控件 - >调用()与.NET,C++和Windows窗体的正确用法。如何使用Control-> Invoke()?

例如用这样的方法:

System::Void UI::setStatusText(System::String^ text) { 
    if (this->InvokeRequired) { 
     SetTextDelegate^ d = gcnew SetTextDelegate(this, &UI::setStatusText); 
     statusLabel->Invoke(d, gcnew array<Object^> { text }); // <-- System.ExecutionEngineException 
    } else 
     statusLabel->Text = text; 
} 

是否正确?此方法由任意线程执行,并应更改statusLabel的文本。

的委托声明为:

delegate void SetTextDelegate(System::String^ text); 

但往往我得到了标记线System.ExecutionEngineException。

有什么不对的代码?

感谢, 马丁

编辑:Visual Studio中说,有在mscorlib.dll类型System.ExecutionEngineException和绿色调试箭头指向标线的非托管异常。这是堆栈跟踪:http://sharetext.org/B8RR。但是,我怎么能得到内部的异常?或者,如果这不是由这些代码行引起的,我还能做些什么来弄清楚错误?

+0

异常的信息是什么?它有内部异常吗?如果是,那是什么? – svick

+2

这与代码很不一样。通常在托管堆损坏时生成FEEE。这是通过较早运行的代码完成的,通常是本机代码。 –

回答

0

委托代码似乎是正确的。但我不明白你在做什么。您正在创建一个创建相同函数setStatusText的委托!我认为你正在尝试做这样的事情:

delegate void SetTextDelegate(); 

System::Void UI::setStatusText(System::String^ text) 
{ 
    statusLabel->Text = text 
} 

System::Void UI::ANOTHER_FUNCTION(System::String^ text) 
{ 
    SetTextDelegate^ d = gcnew SetTextDelegate(this, &UI::setStatusText); 
    statusLabel->Invoke(d, gcnew array<Object^> { text }); 
} 
相关问题