2011-12-01 44 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 }); 
} 
相關問題